The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
More than transposing! bulash UNIX Desktop for Dummies Questions & Answers 3 04-11-2008 02:20 PM
Transposing string unibboy Shell Programming and Scripting 3 02-13-2008 02:12 PM
Major Awk problems (Searching, If statements, transposing etc.) Blivo Shell Programming and Scripting 2 09-05-2007 03:41 AM
file transposing mskcc Shell Programming and Scripting 24 08-04-2005 08:23 AM
transposing letters myscsa2004 Shell Programming and Scripting 4 05-12-2004 07:11 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-15-2006
Registered User
 

Join Date: Sep 2006
Posts: 10
Another transposing issue

Hello

I need to sort a file with data such as so it breaks on column 1 and all the data in column 2 is sorted into rows with a unique column 1:
1 5
1 6
1 7
2 3
2 4
3 7
3 0
3 9

So it comes out as:
1 5 6 7
2 3 4
3 7 0 9

I've tried many iterations of nawk but can't get it working!!!

Thanks in advance!
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 09-15-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 2,991
what have you tried so far?
Reply With Quote
  #3 (permalink)  
Old 09-15-2006
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Code:
awk '
{
   if ( arr[$1] == "" )
        arr[$1]=$1 " " $2
   else
        arr[$1]=arr[$1] " " $2
}
END{
   for( key in arr)
        print arr[key]
}
' file
Reply With Quote
  #4 (permalink)  
Old 09-15-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 2,991
Quote:
Originally Posted by anbu23
Code:
awk '
{
   if ( arr[$1] == "" )
        arr[$1]=$1 " " $2
   else
        arr[$1]=arr[$1] " " $2
}
END{
   for( key in arr)
        print arr[key]
}
' file
and how does this accomplish what the OP wants?
Reply With Quote
  #5 (permalink)  
Old 09-15-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 2,991
here's one way of sorting by both rows [by $1] and columns within the rows:

nawk -f steve.awk steve.txt | sort -k 1n,1

steve.awk:
Code:
function isort(A,n,     i,j,t) {
    for (i = 2; i <= n; i++)
        for (j = i; j > 1 && A[j-1] > A[j]; j--) {
              # swap A[j-1] and A[j]
              t = A[j-1]; A[j-1] = A[j]; A[j] = t
        }
}

{
  arr[$1] = ($1 in arr) ? arr[$1] OFS $2 : $2
}

END {
  for ( i in arr ) {
    n=split(arr[i], tmpA, OFS)
    isort(tmpA, n)
    printf("%s%s", i, OFS )
    for(j=1; j <= n; j++)
       printf("%s%s", tmpA[j], (j==n) ? "\n" : OFS)
  }
}
Reply With Quote
  #6 (permalink)  
Old 09-15-2006
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Quote:
Originally Posted by anbu23
Code:
awk '
{
if ( arr[$1] == "" )
arr[$1]=$1 " " $2
else
arr[$1]=arr[$1] " " $2
}
END{
for( key in arr)
print arr[key]
}
' file



and how does this accomplish what the OP wants?
Did you try running this code?
This code will give what the output he has given.
if he wants second column to be sorted
sort -kn1 -kn2 file | awk ' ...'

Last edited by anbu23; 09-15-2006 at 12:08 PM.
Reply With Quote
  #7 (permalink)  
Old 09-15-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 2,991
well..... even assuming fixing the 'sort' options - these syntax is illegal on Sun/Solaris....
Also assuming the OP's sample input file [steve.txt]...
Code:
$ sort -n -k 1,1 -k 2,2 steve.txt | nawk -f steve1.awk 
2 3 4
3 0 7 9
1 5 6 7
are you seeing different results?

Last edited by vgersh99; 09-15-2006 at 01:37 PM.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:43 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0