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!

data organisation using awk

Status
Not open for further replies.

jmk2

Technical User
Apr 8, 2005
5
CA
Hi, can anyone help me? I have input in the form:

1 67
2 45
3 34
4 78
1 36
2 56
3 36
4 47
1 48
2 35
3 79
4 23
.
.
.

and I want output as such:

1 67 36 48
2 45 56 35
3 34 36 79
4 78 47 23

Thank you for your help!
 
A non awk method if you don't care what are the numbers are in:

#!/bin/ksh

sort datafile |
while read f1 f2
do
if [[ "$f1" != "$f1_buf" ]]
then
printf "\n%s %s " $f1 $f2
f1_buf=$f1
else
printf "%s " $f2
fi
done
 
Thanks "olded", this is a great start. Now I just need help to make the output sorted in the order I need (see my original message). Any hints to do this? Thanks again!
 
A common way to save the integrity of the non sort columns is to add a dummy column, and then sort on it after sorting the primary. Here I'm adding a new dummy column to your datafile - using cat -n - which then becomes the column 1. Your primary sort is column 2; sorting on column 2 retains your numeric order. I hope this explanation made sense:


cat -n datafile|sort -k 2,2 -k 1,1 |
while read f1 f2 f3
do
if [[ "$f2" != "$f2_buf" ]]
then
printf "\n%s %s " $f2 $f3
f2_buf=$f2
else
printf "%s " $f3
fi
done

 
thanks again, you've made my life a lot easier!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top