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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing Out Selected Fields from Different Lines 1

Status
Not open for further replies.

thunderkid

Technical User
Oct 26, 2000
54
US
I have a special challenge in working with extracting data from text files. Below is the format of text file that I would like to extract data:

1111 bbbbbbbbb
3333333333333333333333333333333333333
ddddd 55555 eeeeee 777777

2222 cccccccc
8888888888888888888888888888888888888
fffff 99999 gggggg 999999

Desired output:
1111 bbbbbbbbb 55555 777777
2222 ccccccccc 99999 999999

Any help or assistance is appreciated.



 
Hi Thunderkid!

This is a real simple way to do it by setting the record
separator to one or more blank lines as in your example:

awk 'BEGIN{RS=""}

{
output = sprintf("%s %s %s %s\n", $1, $2, $5, $7 )
print output

}' filename

By setting RS to null it allows the three lines to become
one record and you can parse it accordingly. Format the
sprintf as you wish- I used your example of 1 space, 2
spaces, 3 spaces (for example).

Hope this helps!



flogrr
flogr@yahoo.com

 
Actually it's even easier:

awk 'BEGIN{RS=""}

{
print $1" "$2" "$5" "$7

}' filename


Jesse


flogrr
flogr@yahoo.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top