![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | 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 here. |
|
|
||||
| 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 03: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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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! |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
what have you tried so far?
|
|
#3
|
|||
|
|||
|
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
|
||||
|
||||
|
Quote:
|
|
#5
|
||||
|
||||
|
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
|
|||
|
|||
|
Quote:
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. |
|
#7
|
||||
|
||||
|
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 Last edited by vgersh99; 09-15-2006 at 01:37 PM. |
||||
| Google The UNIX and Linux Forums |