Unix C++
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Doubts regarding paste command

5 posters

Go down

Doubts regarding paste command Empty Doubts regarding paste command

Post by jennyinhere Thu Mar 04, 2010 2:21 pm

Can the paste command be used to display fields extracted using cut command?

The following is a question which requires this...

Create a file student under your home directory. Input 12 lines for the name, age, roll number and sex. Use ‘|’ as the field separator.
Use cut and paste commands to alter the field ordering in student.

jennyinhere

Posts : 24
Points : 30
Join date : 2010-02-26
Age : 35

Back to top Go down

Doubts regarding paste command Empty Re: Doubts regarding paste command

Post by Maithreyi Thu Mar 04, 2010 3:58 pm

[335818@oracleclient ~]$ cat > students
Akalya:03:21:F
Devi:21:22:F
Mahesh:43:22:M
Ramesh:67:22:M
[335818@oracleclient ~]$ cut -d : -f 1,4 students > temp1
[335818@oracleclient ~]$ cut -d : -f 3,2 students > temp2
[335818@oracleclient ~]$ cat temp1
Akalya:F
Devi:F
Mahesh:M
Ramesh:M
[335818@oracleclient ~]$ cat temp2
03:21
21:22
43:22
67:22
[335818@oracleclient ~]$ paste -d : temp1 temp2 > students
[335818@oracleclient ~]$ cat students
Akalya:F:03:21
Devi:F:21:22
Mahesh:M:43:22
Ramesh:M:67:22

It is possible to combine these separate commands into a single line I guess.Hope it helps!

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 36
Location : Haldia

Back to top Go down

Doubts regarding paste command Empty Re: Doubts regarding paste command

Post by jennyinhere Thu Mar 04, 2010 4:12 pm

yep.. I did this too
Can this be done without using tmp files?

jennyinhere

Posts : 24
Points : 30
Join date : 2010-02-26
Age : 35

Back to top Go down

Doubts regarding paste command Empty Re: Doubts regarding paste command

Post by akalya Thu Mar 04, 2010 4:14 pm

try this one:
$ cat tmp
1 2 3 4 5
a b c d e

$paste >(cut -d" " -f1,3,5 tmp) > (cut -d" " -f2,4 tmp)
1 3 5
a c e
2 4
b d

The results of cut commands are placed one behind the other....trying to get them placed in a single row...hope it helps for now!!!!

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

Back to top Go down

Doubts regarding paste command Empty Re: Doubts regarding paste command

Post by jennyinhere Thu Mar 04, 2010 4:19 pm

I am getting this error

-bash: syntax error near unexpected token `('

jennyinhere

Posts : 24
Points : 30
Join date : 2010-02-26
Age : 35

Back to top Go down

Doubts regarding paste command Empty Re: Doubts regarding paste command

Post by akalya Thu Mar 04, 2010 4:25 pm

make sure of the spacings
$paste >(cut -d" " -f1,3,5 tmp) >(cut -d" " -f2,4 tmp)

Rolling Eyes

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

Back to top Go down

Doubts regarding paste command Empty Re: Doubts regarding paste command

Post by jennyinhere Thu Mar 04, 2010 4:36 pm

got it

jennyinhere

Posts : 24
Points : 30
Join date : 2010-02-26
Age : 35

Back to top Go down

Doubts regarding paste command Empty Re: Doubts regarding paste command

Post by akalya Thu Mar 04, 2010 4:40 pm

gud Smile
morever paste can only place back things vertically.....so we can only have it displayed one behind another...

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

Back to top Go down

Doubts regarding paste command Empty Re: Doubts regarding paste command

Post by jennyinhere Thu Mar 04, 2010 4:51 pm

okie Smile

jennyinhere

Posts : 24
Points : 30
Join date : 2010-02-26
Age : 35

Back to top Go down

Doubts regarding paste command Empty Re: Doubts regarding paste command

Post by Christopher Thu Mar 04, 2010 5:12 pm

i don think it will work. it is not pasting vertically. output of second cut command is just appended to the first.
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

Doubts regarding paste command Empty Re: Doubts regarding paste command

Post by jennyinhere Thu Mar 04, 2010 5:42 pm

I would like the fields to be displayed in a different order. Is that possible?

Lets assume that the student file is

a | 20 | 1 | f
b | 21 | 2 | m
c | 20 | 3 | f
d | 22 | 4 | f

How do I interchange fields by just using cut and paste

jennyinhere

Posts : 24
Points : 30
Join date : 2010-02-26
Age : 35

Back to top Go down

Doubts regarding paste command Empty Re: Doubts regarding paste command

Post by akalya Thu Mar 04, 2010 5:45 pm

could u please tell how u want the output to look like???

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

Back to top Go down

Doubts regarding paste command Empty Re: Doubts regarding paste command

Post by jennyinhere Thu Mar 04, 2010 5:52 pm

a | 1 | 20 | f
b | 2 | 21 | m
c | 3 | 20 | f
d | 4 | 22 | f

Interchanging columns

jennyinhere

Posts : 24
Points : 30
Join date : 2010-02-26
Age : 35

Back to top Go down

Doubts regarding paste command Empty Re: Doubts regarding paste command

Post by Maithreyi Thu Mar 04, 2010 5:54 pm

The reply I had given does the reordering.

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 36
Location : Haldia

Back to top Go down

Doubts regarding paste command Empty Re: Doubts regarding paste command

Post by akalya Thu Mar 04, 2010 5:57 pm

ya maithreyi's reply would be more appropriate in that case

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

Back to top Go down

Doubts regarding paste command Empty Re: Doubts regarding paste command

Post by Maithreyi Thu Mar 04, 2010 6:03 pm

Doing it without a temp file might not be possible because cut 4,2,1,3 doesn't change the ordering.

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 36
Location : Haldia

Back to top Go down

Doubts regarding paste command Empty Re: Doubts regarding paste command

Post by Maithreyi Thu Mar 04, 2010 6:06 pm

Doing it without a temp file may not work because cut 4,2,1,3 does not do reordering.

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 36
Location : Haldia

Back to top Go down

Doubts regarding paste command Empty Re: Doubts regarding paste command

Post by DineshThangaraju Thu Mar 04, 2010 6:09 pm

[336484@oracleclient fo]$ cut -d"|" -f3 student.txt > t1
[336484@oracleclient fo]$ cut -d"|" -f1 student.txt > t2
[336484@oracleclient fo]$ cut -d"|" -f4 student.txt > t3
[336484@oracleclient fo]$ cut -d"|" -f2 student.txt > t4
[336484@oracleclient fo]$ paste t1 t2 t3 t4 > t5
[336484@oracleclient fo]$ cat t5
1 a f 20
2 b m 21
3 c f 20
4 d f 22
[336484@oracleclient fo]$ paste -d"-" t1 t2 t3 t4 > t5
[336484@oracleclient fo]$ cat t5
1-a-f-20
2-b-m-21
3-c-f-20
4-d-f-22

DineshThangaraju

Posts : 18
Points : 19
Join date : 2010-02-26
Age : 35
Location : India

Back to top Go down

Doubts regarding paste command Empty Re: Doubts regarding paste command

Post by Christopher Wed Mar 10, 2010 6:24 pm

this solution is really complex, not a efficient way of programming. but still it doesn't use any temp file to alter fields in a file.

Code:
$ cat stud.x
Arun|20|1001|M
Bala|21|1002|M
Chris|20|1003|M
Deepan|21|1004|M
Code:
$ cat sam.sh
i=0
for a in `cut -d '|' -f 3 stud.x`
do
j=0
for b in `cut -d '|' -f 1,2,4 stud.x`
do
if [ $i -eq $j ]
then
echo $a"|"$b
fi
j=`expr $j + 1`
done
i=`expr $i + 1`
done
Code:
$ sh sam.sh
1001|Arun|20|M
1002|Bala|21|M
1003|Chris|20|M
1004|Deepan|21|M
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

Doubts regarding paste command Empty Re: Doubts regarding paste command

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum