Posts Topics Forums Images
Search videos from message boards Videos Search messages from microblogs Microblogs Search messages from imdb.com Imdb Search messages from yuku.com Yuku Search messages from lefora.com (free forums) Lefora
My account: Login | Sign Up
Loading... 

Thread: Append value of a row to subsequent rows

Started 1 month, 1 week ago by surabhinair
Hi, I have source file where in the data is in the following format. 01,America,0001,AUH 03,F,F ,001,000,012,000 04,F,F ,023,000,012,000 03,F,A ,002,005,012,005,Y 04,F,A ,023,000,012,000,000 03,F,R ,003,007,007,007,Y,002 04,F,R ,023,000,007,002,002,002 03,F,O ,004,000,000,000,Y,003 04,F,O ,023,000,000,000,000 01,EGYPT,0001,AUH 03,F,F ,001,001,012,001,Y...
Site: LinuxQuestions.org - where Linux users come for help  LinuxQuestions.org - where Linux users come for help - site profile
Forum: Other *NIX  Other *NIX - forum profile
Total authors: 4 authors
Total thread posts: 5 posts
Thread activity: no new posts during last week
Domain info for: linuxquestions.org

Other posts in this thread:

druuna replied 1 month, 1 week ago
Hi, Something like this maybe: Code: #!/bin/bash awk ' BEGIN { FS = "," } { if ( $1 == "01" ) { Appender=$2 ; print $0 } if ( $1 != "01" ) { print $0","Appender } } ' infile Testrun: Code: $ cat infile 01,America,0001,AUH 03,F,F ,001,000,012,000 04,F,F ,023,000,012,000 03,F,A ,002,005,012,005,Y 04,F,A ,023,000,012,000,000 03,F,R ,003,007,007,007,Y,...

ghostdog74 replied 1 month, 1 week ago
Code: $ awk -F"," '$1=="01"{s=$2;print;next}{print $0","s}' file 01,America,0001,AUH 03,F,F ,001,000,012,000,America 04,F,F ,023,000,012,000,America 03,F,A ,002,005,012,005,Y,America 04,F,A ,023,000,012,000,000,America 03,F,R ,003,007,007,007,Y,002,America 04,F,R ,023,000,007,002,002,002,America 03,F,O ,004,000,000,000,Y,003,America 04,F,O ,023,000,000,000,000,America 01,EGYPT,0001,AUH ...

druuna replied 1 month, 1 week ago
@ghostdog74: That's a nice, short one-liner!

Telemachos replied 1 month, 1 week ago
For fun, a Perl option: Code: perl -ple 'if (m/^01/) { $loc = (split /,/)[1] } else { $_ = "$_,$loc" }' file More awk -ishly Perl: Code: perl -F, -plae 'if ($F[0] == '01') { $loc = $F[1] } else { $_ = "$_,$loc" }' file (I like the first version better since it only splits the '01' lines, and in a big file that might amount to a significant difference.)

 

Top contributing authors

Name
Posts
druuna
2
user's latest post:
Append value of a row to...
Published (2009-10-28 07:04:00)
@ghostdog74: That's a nice, short one-liner!
surabhinair
1
user's latest post:
Append value of a row to...
Published (2009-10-28 06:07:00)
Hi, I have source file where in the data is in the following format. 01,America,0001,AUH 03,F,F ,001,000,012,000 04,F,F ,023,000,012,000 03,F,A ,002,005,012,005,Y 04,F,A ,023,000,012,000,000 03,F,R ,003,007,007,007,Y,002 04,F,R ,023,000,007,002,002,002 03,F,O ,004,000,000,000,Y,003 04,F,O ,023,000,000,000,000 01,EGYPT,0001,AUH 03,F,F ,001,001,012,001,Y 04,F,F ,023,000,012,001,001 01,SPAIN,0001,AUH 03,F,A ,021,000,012,000 04,F,F...
Telemachos
1
user's latest post:
Append value of a row to...
Published (2009-10-28 20:07:00)
For fun, a Perl option: Code: perl -ple 'if (m/^01/) { $loc = (split /,/)[1] } else { $_ = "$_,$loc" }' file More awk -ishly Perl: Code: perl -F, -plae 'if ($F[0] == '01') { $loc = $F[1] } else { $_ = "$_,$loc" }' file (I like the first version better since it only splits the '01' lines, and in a big file that might amount to a significant difference.)
ghostdog74
1
user's latest post:
Append value of a row to...
Published (2009-10-28 06:56:00)
Code: $ awk -F"," '$1=="01"{s=$2;print;next}{print $0","s}' file 01,America,0001,AUH 03,F,F ,001,000,012,000,America 04,F,F ,023,000,012,000,America 03,F,A ,002,005,012,005,Y,America 04,F,A ,023,000,012,000,000,America 03,F,R ,003,007,007,007,Y,002,America 04,F,R ,023,000,007,002,002,002,America 03,F,O ,004,000,000,000,Y,003,America 04,F,O ,023,000,000,000,000,America...

Related threads on "LinuxQuestions.org - where Linux users come for help":

Related threads on other sites:

Thread profile page for "Append value of a row to subsequent rows" on http://www.linuxquestions.org. This report page is a snippet summary view from a single thread "Append value of a row to subsequent rows", located on the Message Board at http://www.linuxquestions.org. This thread profile page shows the thread statistics for: Total Authors, Total Thread Posts, and Thread Activity