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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

convert columns into row with awk

Reply to Message Icon

Name: MYS
Date: May 29, 2008 at 02:38:46 Pacific
OS: windows
CPU/Ram: 3G
Product: 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



Sponsored Link
Ads by Google

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

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


0

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


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



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: convert columns into row with awk

please help me with awk, please www.computing.net/answers/unix/please-help-me-with-awk-please/3813.html

find the number of line with awk. www.computing.net/answers/unix/find-the-number-of-line-with-awk/4554.html

converting month into numbers www.computing.net/answers/unix/converting-month-into-numbers/6869.html