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

Reformat file - awk? 1

Status
Not open for further replies.

kasparov

Programmer
Feb 13, 2002
203
GB
Can anyone help me with this? I have an input file similar to this:

uid=ADMIN
first_name=Admin
last_name=test user
uid=Administrator
first_name=Administrator
last_name=Administrator
uid=Guest
last_name=Guest
uid=anonymous
uid=config_fwk_service
uid=pcd_service
uid=ume_service
uid=11687
first_name=Patti
last_name=Smith
uid=11693
first_name=Joe
last_name=Strummer
uid=11719
first_name=Mick
last_name=Jagger
uid=11728
last_name=Lennon
uid=11729
first_name=Eric
last_name=Clapton

And I want it to be transformed to this:

ADMIN Admin test user
Administrator Administrator Administrator
Guest Guest
anonymous
config_fwk_service
pcd_service
ume_service
11687 Patti Smith
11693 Joe Strummer
11719 Mick Jagger
11728 Lennon
11729 Eric Clapton

So some records have missing fields (like Lennon's first name) & some fields have spaces (like "test user"). The field separators are tabs.

I'm assuming awk is the best tool to use here but don't know where to start.

Thanks, Chris
 
You may try something like this:
awk -F= '
/^uid/{if(u)print u"\t"f"\t"l;u=$2;f=l=""}
/^first/{f=$2}
/^last/{l=$2}
END{if(u)print u"\t"f"\t"l}
' /path/to/inputfile


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top