|
google site
|
|||||||
| Forums | Register | Blog | Man Pages | Forum Rules | Links | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Convert row data to column data
Hi Guys, I have a file as follows: Code:
a 1 b 786 c 90709 d 99 a 9875 b 989 c 887 d 111 I want: Code:
a 1 9875 b 786 989 c 90709 887 d 99 111 So I want the row data to appear in columns for a,b,c and d. I have more than two sets of (a,b,c,d) data so I am looking for a general solution with "n" sets of (a,b,c,d) data. Also (a,b,c,d) is just an example and I want to use the script for (n) number of groups (not just 4 i.e. a,b,c,d). I tried the paste command but this data is in the same file. Thanks. |
| Sponsored Links | ||
|
|
|
#2
|
|||
|
|||
|
Code:
awk '{a[$1]=a[$1]?a[$1] FS $NF:$0}END{for(i in a) print a[i]}' infile | sort---------- Post updated at 09:26 PM ---------- Previous update was at 09:25 PM ---------- Use gawk, nawk or /usr/xpg4/bin/awk on Solaris. |
|
#3
|
|||
|
|||
|
a perl solution which you can extend easily for similar other cases too.. Code:
$ cat t.pl
while(<>) {
# take the first word, use it as key and build a hash.
( $key, $value ) = /^(\w+)\s+(.*)$/;
push @{$hash{$key}}, $value;
}
while ( ($key, $value) = each %hash ) {
print "$key ";
print "@$value";
print "\n";
}Code:
$ cat input-file a 1 b 786 c 90709 d 99 a 9875 b 989 c 887 d 111 $ perl t.pl input-file c 90709 887 a 1 9875 b 786 989 d 99 111 |
|
#4
|
|||
|
|||
|
Thanks guys.
Can I use cut and paste? For example can I cut the values for the second set of a,b,c,d numbers and paste them in the second column, the third set of a,b,c,d numbers and paste them in the third column and so on? |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Extract data based on match against one column data from a long list data | patrick87 | Shell Programming and Scripting | 12 | 11-17-2009 03:27 AM |
| Linear data to column data..script help seeked | ak835 | Shell Programming and Scripting | 1 | 09-02-2009 01:32 PM |
| Convert two column data into 8 columns | NickC | Shell Programming and Scripting | 8 | 06-28-2008 11:19 AM |
| Put raw data to column data | Nayanajith | Shell Programming and Scripting | 7 | 03-20-2007 03:13 AM |
| Convert Binary data to ascii data | krishna | UNIX for Advanced & Expert Users | 4 | 11-05-2004 03:12 PM |