Computing.Net > Forums > Unix > convert columns into row with awk

convert columns into row with awk

Reply to Message Icon

Original Message
Name: MYS
Date: May 29, 2008 at 02:38:46 Pacific
Subject: convert columns into row with awk
OS: windows
CPU/Ram: 3G
Model/Manufacturer: Dell
Comment:

my file contains data in the following format

1951 1 10
1951 1 20
1951 1 30
1951 1 40

1952 1 12
1952 1 22
1952 1 32
1952 1 42
and so on

I want to look like

1951 10 20 30 40
1952 12 22 32 42

each year (1951 to 2004) has 54 rows and I want values under each row to be in one row and the years at 1st column like I wrote above.

I really need it fast


Report Offensive Message For Removal


Response Number 1
Name: James Boothe
Date: May 29, 2008 at 14:04:23 Pacific
Reply: (edit)

awk '{
if (NF==0)
   next
if ($1==holdyear)
   printf " " $3
else
  {printf "%s%s %s",LF,$1,$3
   holdyear=$1
   LF="\n"}
}
END {printf "\n"}' filein


Report Offensive Follow Up For Removal

Response Number 2
Name: ghostdog
Date: May 30, 2008 at 22:10:01 Pacific
Reply: (edit)


awk '!/^$/{
a[$1]=a[$1]" "$3
}
END {
for ( i in a) {
print i,a[i]
}
}
' file



Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: convert columns into row with awk

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 6 Days.
Discuss in The Lounge