I have the following awk statement that computes rank of a column and sorts it based on rank.
sort -k6 -n file | awk '$6 != prev { rank = NR }{ print $1, $6, rank ; prev = $6 }'
I need the above code to accept variables for the column in the sort statement ("-k6").
Is there a better way of going about this so that variables can be used?
Please provide a sample of input and desired output..
input:
2010 3.32 1.41 1.99 1.99 2.88 2.95 3.00 2.53 1.58 1.08 2.29 2.53
2011 2.40 2.05 2.41 2.59 2.83 2.64 3.22 2.00 2.78 2.15 3.07 1.50
2012 1.88 2.66 3.03 3.30 3.00 3.01 2.31 2.96 2.39 1.34 2.68 2.06
desired output:
2011 2.83 1
2010 2.88 2
2012 3.00 3
Ditto
May 16, 2014, 9:08am
4
Does this help?
(I found it easier with cut, however, the triple space seperators confuse it .. so I put the sed in there to trim it down. adjust accordingly )
> more in.txt
2010 3.32 1.41 1.99 1.99 2.88 2.95 3.00 2.53 1.58 1.08 2.29 2.53
2011 2.40 2.05 2.41 2.59 2.83 2.64 3.22 2.00 2.78 2.15 3.07 1.50
2012 1.88 2.66 3.03 3.30 3.00 3.01 2.31 2.96 2.39 1.34 2.68 2.06
> more tt
echo "Enter column #"
read colx
sort -k${colx} -n in.txt | sed "s/ / /g" |cut -d" " -f 1,${colx}
> ksh tt
Enter column #
6
2011 2.83
2010 2.88
2012 3.00
> ksh tt
Enter column #
2
2012 1.88
2011 2.40
2010 3.32
RudiC
May 16, 2014, 9:33am
5
How about
COL=6
sort -k$COL -n file | awk -vCOL=$COL '$COL != prev { rank = NR }{ print $1, $COL, rank ; prev = $COL }'
2011 2.83 1
2010 2.88 2
2012 3.00 3
Thanks to all for the reply
I discovered that my environment variables were set up in a way to for awk to handle properly, but not the sort command.
Here is the code that works for me.
sort -k${column} -n file | awk ''${awkcol}' != prev { rank = NR }{ print $1, '${awkcol}', rank ; prev = '${awkcol}' }'
Good that it works. Have a look at this adjusted version of RudiC's suggestion, which is the same solution, but it uses a better method of passing the variable to awk...
sort -k"$column" -n file | awk -v COL="$awkcol" 'COL != prev { rank = NR }{ print $1, COL, rank ; prev = COL }'