LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > Other *NIX
User Name
Password
Other *NIX This forum is for the discussion of any UNIX platform that does not have its own forum. Examples would include HP-UX, IRIX, Darwin, Tru64 and OS X.

Notices


Reply
  Search this Thread
Old 10-27-2009, 06:07 AM   #1
surabhinair
LQ Newbie
 
Registered: Oct 2009
Posts: 1

Rep: Reputation: 0
Smile Append value of a row to subsequent rows


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 ,023,000,017,000
03,F,A ,002,105,012,008,Y
04,F,A ,023,000,012,000,000

I want to append the second column value of records starting with 01 to subsequent records till the next o1 value.

desired output:

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
03,F,F ,001,001,012,001,Y,EGYPT
04,F,F ,023,000,012,001,001,EGYPT
01,SPAIN,0001,AUH
03,F,A ,021,000,012,000,SPAIN
04,F,F ,023,000,017,000,SPAIN
03,F,A ,002,105,012,008,Y,SPAIN
04,F,A ,023,000,012,000,000,SPAIN

Its to be noted that the order of records matter and the count of records between 2 subsequent 01 record is is not fixed.

Any help on the same is appreciated.
 
Old 10-27-2009, 06:20 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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,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 ,023,000,017,000
03,F,A ,002,105,012,008,Y
04,F,A ,023,000,012,000,000

$ ./tets

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 
03,F,F ,001,001,012,001,Y,EGYPT
04,F,F ,023,000,012,001,001,EGYPT
01,SPAIN,0001,AUH 
03,F,A ,021,000,012,000,SPAIN
04,F,F ,023,000,017,000,SPAIN
03,F,A ,002,105,012,008,Y,SPAIN
04,F,A ,023,000,012,000,000,SPAIN
Hope this helps.
 
Old 10-27-2009, 06:56 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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
03,F,F ,001,001,012,001,Y,EGYPT
04,F,F ,023,000,012,001,001,EGYPT
01,SPAIN,0001,AUH
03,F,A ,021,000,012,000,SPAIN
04,F,F ,023,000,017,000,SPAIN
03,F,A ,002,105,012,008,Y,SPAIN
04,F,A ,023,000,012,000,000,SPAIN
 
Old 10-27-2009, 07:04 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@ghostdog74: That's a nice, short one-liner!
 
Old 10-27-2009, 08:07 PM   #5
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
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.)

Last edited by Telemachos; 10-27-2009 at 08:33 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Paste each single row from n separate text files into a new file containing n rows Mike_V Programming 11 04-27-2009 11:51 PM
Compare two fields on consecutive rows and print the two rows aditi_borkar Linux - Newbie 3 04-09-2009 05:49 AM
Shell script to parse csv-like output, row by row utahnix Linux - General 8 12-08-2007 05:03 AM
[C++] append to each row of a text files sylvaticus Programming 2 10-18-2007 07:22 AM
Mounting 2nd and subsequent HDDs confused_bof Linux - Newbie 3 09-12-2003 06:12 PM

LinuxQuestions.org > Forums > Other *NIX Forums > Other *NIX

All times are GMT -5. The time now is 02:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration