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

Append column two if line one matches 1

Status
Not open for further replies.
Apr 8, 2014
13
US
Sample file:
host1 peach
host1 cherry
host2 pear
host3 apple
host4 orange
host4 banana
host4 plum

Desired output:
host1 peach cherry
host2 pear
host3 apple
host4 orange banana plum

Tried this but can't get it to work.
Code:
awk -F' * *' '{a[$1 " " $2]=a[$1 " " $2] " " $3} END {for (k in a) {print k a[k]}}'
 
Hi

Why setting a field separator ?

Anyway, the problem is that you seem to count 3 fields, while with or without setting the field separator, you still have only 2 :
Code:
awk '{a[$1]=a[$1] " " $2} END {for (k in a) {print k a[k]}}'
Tested with [tt]gawk[/tt], [tt]mawk[/tt] and original [tt]awk[/tt].


Feherke.
feherke.ga
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top