×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

awk command to rearrange data

awk command to rearrange data

awk command to rearrange data

(OP)
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.

RE: awk command to rearrange data

Hi

CODE

awk '$2=="PRINT"{p=$1;next}{print p,$0}' /path/to/input 

Feherke.
feherke.github.io

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close