Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
google site



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Display Modes
  #1  
Old 02-08-2010
Registered User
 

Join Date: Nov 2008
Posts: 183
Thanks: 0
Thanked 0 Times in 0 Posts
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  
Old 02-08-2010
danmero danmero is offline Forum Advisor  
 

Join Date: Nov 2007
Location: H3X
Posts: 1,713
Thanks: 0
Thanked 9 Times in 9 Posts

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  
Old 02-08-2010
thegeek thegeek is offline Forum Advisor  
Registered User
 

Join Date: Apr 2009
Location: /usr/bin/vim
Posts: 870
Thanks: 2
Thanked 10 Times in 8 Posts
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  
Old 02-09-2010
Registered User
 

Join Date: Nov 2008
Posts: 183
Thanks: 0
Thanked 0 Times in 0 Posts
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
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 07:38 AM.