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

awk command to rearrange data

Status
Not open for further replies.

SDCSA

Programmer
May 8, 2003
22
0
0
US
Dear members,

I have a text file with several lines of output as below.
Each header line has HEADER number and word PRINT.

I will need to grep the first word in the line with the keyword PRINT i.e HEADER1, HEADER2 and basically prefix as column1 in each line underneath. Should do this until the next PRINT is found, this marks the end of that dataset and so on.... Can you please suggest a way/command to achieve this ?

Source file :

HEADER1 PRINT
text1_1 text2_1 text3_1
text1_1 text2_1 text3_1
text1_1 text2_1 text3_1
HEADER2 PRINT
text1_2 text2_2 text3_2
text1_2 text2_2 text3_2
text1_2 text2_2 text3_2
HEADER3 PRINT
text1_3 text2_3 text3_3
text1_3 text2_3 text3_3
text1_3 text2_3 text3_3

Necessary Output :

HEADER1 text1_1 text2_1 text3_1
HEADER1 text1_1 text2_1 text3_1
HEADER1 text1_1 text2_1 text3_1
HEADER2 text1_2 text2_2 text3_2
HEADER2 text1_2 text2_2 text3_2
HEADER2 text1_2 text2_2 text3_2
HEADER3 text1_3 text2_3 text3_3
HEADER3 text1_3 text2_3 text3_3
HEADER3 text1_3 text2_3 text3_3

Best Regards.
 
Hi

Code:
awk '[navy]$2[/navy][teal]==[/teal][i][green]"PRINT"[/green][/i][teal]{[/teal][navy]p[/navy][teal]=[/teal][navy]$1[/navy][teal];[/teal][b]next[/b][teal]}{[/teal][b]print[/b] p[teal],[/teal][navy]$0[/navy][teal]}[/teal]' /path/to/input

Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top