Operating System - HP-UX
1833462 Members
2778 Online
110052 Solutions
New Discussion

Re: file manipulation with scripts

 
SOLVED
Go to solution
Theresa Patrie
Regular Advisor

file manipulation with scripts

Hi All...
I have a file that looks like this:

User: frencht
License: panth.5.0
License: panth_des.5.0
User: miedicr
License: panth_des.5.0
User: patriet
License: panth.5.0
License: panth_des.5.0
User: gormanl
License: panth.5.0
License: panth_des.5.0
User: bozzad
License: panth_des.5.0

It is a username followed by any number of license lines for licenses that are checked out. How can I change the format of this file so that it looks like this:

User: frencht panth.5.0 panth_des.5.0
User: miedicr panth_des.5.0
User: patriet panth.5.0 panth_des.5.0
User: gormanl panth.5.0 panth_des.5.0
User: bozzad panth_des.5.0

I am sure there is a relatively simple way, but I've been messing around with awk and sed and haven't quite got it. Please help!
Thanks,
Theresa
This is my easy job!
4 REPLIES 4
Victor Fridyev
Honored Contributor
Solution

Re: file manipulation with scripts

Hi,
This should work:

awk -F: '/User/ {printf("\nUser:%s",$2);continue}
{printf("%s ",$2)}
END {print ""}' FILE | awk 'NR>1 {print}'

If empty string in the output is not a problem, remove second awk
HTH
Entities are not to be multiplied beyond necessity - RTFM
Theresa Patrie
Regular Advisor

Re: file manipulation with scripts

Thank You Victor...it absolutely does work...perfectly!
This is my easy job!
Muthukumar_5
Honored Contributor

Re: file manipulation with scripts

We can do it simply as,

awk -F ":" '{ if ( $1 == "User" ) printf("\n%s",$2)
if ( $1 == "License" ) printf("%s",$2) } END { print ("\n") }' filename

frencht panth.5.0 panth_des.5.0
miedicr panth_des.5.0
patriet panth.5.0 panth_des.5.0
gormanl panth.5.0 panth_des.5.0
bozzad panth_des.5.0

- muthu
Easy to suggest when don't know about the problem!
Theresa Patrie
Regular Advisor

Re: file manipulation with scripts

Same idea, different syntax. Thanks!
This is my easy job!