Doubts regarding paste command
5 posters
Doubts regarding paste command
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.
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
Re: Doubts regarding paste command
[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!
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
Re: Doubts regarding paste command
yep.. I did this too
Can this be done without using tmp files?
Can this be done without using tmp files?
jennyinhere- Posts : 24
Points : 30
Join date : 2010-02-26
Age : 35
Re: Doubts regarding paste command
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!!!!
$ 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
Re: Doubts regarding paste command
I am getting this error
-bash: syntax error near unexpected token `('
-bash: syntax error near unexpected token `('
jennyinhere- Posts : 24
Points : 30
Join date : 2010-02-26
Age : 35
Re: Doubts regarding paste command
make sure of the spacings
$paste >(cut -d" " -f1,3,5 tmp) >(cut -d" " -f2,4 tmp)
$paste >(cut -d" " -f1,3,5 tmp) >(cut -d" " -f2,4 tmp)
akalya- Posts : 70
Points : 86
Join date : 2010-03-04
jennyinhere- Posts : 24
Points : 30
Join date : 2010-02-26
Age : 35
Re: Doubts regarding paste command
gud
morever paste can only place back things vertically.....so we can only have it displayed one behind another...
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
jennyinhere- Posts : 24
Points : 30
Join date : 2010-02-26
Age : 35
Re: Doubts regarding paste command
i don think it will work. it is not pasting vertically. output of second cut command is just appended to the first.
Re: Doubts regarding paste command
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
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
Re: Doubts regarding paste command
could u please tell how u want the output to look like???
akalya- Posts : 70
Points : 86
Join date : 2010-03-04
Re: Doubts regarding paste command
a | 1 | 20 | f
b | 2 | 21 | m
c | 3 | 20 | f
d | 4 | 22 | f
Interchanging columns
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
Re: Doubts regarding paste command
The reply I had given does the reordering.
Maithreyi- Posts : 76
Points : 142
Join date : 2010-03-03
Age : 36
Location : Haldia
Re: Doubts regarding paste command
ya maithreyi's reply would be more appropriate in that case
akalya- Posts : 70
Points : 86
Join date : 2010-03-04
Re: Doubts regarding paste command
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
Re: Doubts regarding paste command
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
Re: Doubts regarding paste command
[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
[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
Re: Doubts regarding paste command
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
Similar topics
» putty cpy paste
» Cut command - problems
» tee command doubt
» Command error
» What s d diff between 'sed' and 'tr' command??
» Cut command - problems
» tee command doubt
» Command error
» What s d diff between 'sed' and 'tr' command??
Permissions in this forum:
You cannot reply to topics in this forum
|
|