Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use awk to append lines matching regex to previous line of file 2

Status
Not open for further replies.

DubiousAlliance

Technical User
Oct 15, 2009
8
US
Need some guidance on how to use awk to append the line(s) that match a regexp to the previous unmatched line within a file.

For example, I have a comma delimited file that looks like:

01,123456789,123456789,,1234,1234,,12345
02,12345654321,12345,,,12345,12345
99,00000,00000,000000,,,000
99,11111,11111,1111,,,111,11
10,123456,123456,654321,654321
99,00000,00000,00000,,,00000
11,123456,123456,12345654321,,,000

The first two instances of the lines that begin with "99" need to be appended to the line that begins with "02". The third instance of the line that begins with "99" needs to be appended to line that begins with "10".

I've been playing with the awk command trying to use:

awk '/^99/ { print LINE }; {LINE=$0}' < FILE

...But am having trouble with the multiple instance of the "99" line.

Any help appreciated.

Thanks!
 
Try this:

Code:
awk '
        # line does not start with 99, print previous line if there was one, 
        # save new line and skip further processing
        !/^99/ { if (LINE) print LINE; LINE=$0; next }
        # line starts with 99, append it to previous line
        { LINE=LINE $0 }
        # all done, print the last line
        END { print LINE }
' < FILE

Annihilannic.
 
Annihilannic,

Thank you! This worked pretty well.
I added the following line in order to remove the "99" from the line prior to appending it to the previous line:
{ LINE=LINE gsub(/^99/, "", $0) }

...So that the awk script looks like:


awk '
# line does not start with 99
# print previous line if there was one,
# save new line and skip further processing
!/^99/ { if (LINE) print LINE; LINE=$0; next }
# Remove the 99 from beginning of line
{ LINE=LINE gsub(/^99/, "", $0) }
# line starts with 88, append it to previous line
{ LINE=LINE $0 }
# all done, print the last line
END { print LINE }
' < FILE

But now in place of the "99" I am getting a "1" in the appended line.

Any ideas?

Thanks again for the assistance.

 
Okay -
Researching sub & gsub shows that both will return a "1" if a successful substitution occurs. So how can I do the gsub or sub without the return value being inserted into the data?
I'm assuming I can't redirect output to /dev/null or something like that?

Thanks in advance...
 
I'd replace this:
LINE=LINE gsub(/^99/, "", $0)
with this:
sub(/^99/, "", $0);LINE=LINE $0

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV,

That did it! Thanks!

Final script looks like this:

awk '
# line does not start with 99,
# print previous line if there was one,
# save new line and skip further processing
!/^99/ { if (LINE) print LINE; LINE=$0; next }
# remove the 99 from beginning of line
# and append remainder to previous line
{ sub(/^99/, ""); LINE=LINE $0 }
# all done, print the last line
END { print LINE }
' < $FILE >> $CLEAN_DATA

Thanks again!

Regards,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top