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 and shell scripting languages 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 06:20 PM
Transposing string unibboy Shell Programming and Scripting 3 02-13-2008 06:12 PM
Major Awk problems (Searching, If statements, transposing etc.) Blivo Shell Programming and Scripting 2 09-05-2007 07:41 AM
file transposing mskcc Shell Programming and Scripting 24 08-04-2005 12:23 PM
transposing letters myscsa2004 Shell Programming and Scripting 4 05-12-2004 11:11 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-15-2006
stevesmith stevesmith is offline
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!
  #2 (permalink)  
Old 09-15-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
what have you tried so far?
  #3 (permalink)  
Old 09-15-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398

Code:
awk '
{
   if ( arr[$1] == "" )
        arr[$1]=$1 " " $2
   else
        arr[$1]=arr[$1] " " $2
}
END{
   for( key in arr)
        print arr[key]
}
' file

  #4 (permalink)  
Old 09-15-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
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?
  #5 (permalink)  
Old 09-15-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
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)
  }
}

  #6 (permalink)  
Old 09-15-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
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 04:08 PM..
  #7 (permalink)  
Old 09-15-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
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 05:37 PM..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On




All times are GMT -4. The time now is 04:12 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0