Including variable in sort command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Including variable in sort command
# 1  
Old 05-16-2014
Including variable in sort command

I have the following awk statement that computes rank of a column and sorts it based on rank.

Code:
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?
# 2  
Old 05-16-2014
Please provide a sample of input and desired output..
# 3  
Old 05-16-2014
input:

Code:
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:

Code:
2011 2.83 1
2010 2.88 2
2012 3.00 3

# 4  
Old 05-16-2014
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 Smilie )

Code:
> 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

# 5  
Old 05-16-2014
How about
Code:
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

# 6  
Old 05-16-2014
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.

Code:
sort -k${column} -n  file | awk ''${awkcol}' != prev { rank = NR }{ print $1, '${awkcol}', rank ; prev = '${awkcol}' }'

# 7  
Old 05-16-2014
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...
Code:
sort -k"$column" -n file | awk -v COL="$awkcol" 'COL != prev { rank = NR }{ print $1, COL, rank ; prev = COL }'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Including Hash / in sed command filter

Hello All, I want to print data in between two lines in a file sample.txt through more or cat command on the screen. For that I am using below sed command to give the BEGIN and END text. Content of sample.txt server01:~ # cat /proc/mdstat Hello this is a text message 1 Hello this is a... (5 Replies)
Discussion started by: Xtreme
5 Replies

2. Shell Programming and Scripting

A variable is including the Carriage Return char...

Hi all, I'm reading a file with this layout: First_Col Second_Col The Second_Col has values as 1000, -1, 10, 43... While reading the file I'm getting the second column value with awk command, but it is including the CR control char. do item_saved=`echo $b | awk '{print... (4 Replies)
Discussion started by: mrreds
4 Replies

3. Shell Programming and Scripting

How to Sort Floating Numbers Using the Sort Command?

Hi to all. I'm trying to sort this with the Unix command sort. user1:12345678:3.5:2.5:8:1:2:3 user2:12345679:4.5:3.5:8:1:3:2 user3:12345687:5.5:2.5:6:1:3:2 user4:12345670:5.5:2.5:5:3:2:1 user5:12345671:2.5:5.5:7:2:3:1 I need to get this: user3:12345687:5.5:2.5:6:1:3:2... (7 Replies)
Discussion started by: daniel.gbaena
7 Replies

4. Shell Programming and Scripting

executing/including command in mailx subject line

Hi, Is it possible for me to include the results from a command in the subject line? What I am looking to do is get the file count and include it into the subject line as well as the list of files in the body. Example Subject line: Currently 25 files in directory My Code: #!/bin/ksh cd... (2 Replies)
Discussion started by: ozifer
2 Replies

5. Red Hat

Command to find the local harddisks of a server ? Including their size

Hi guys How do I find the harddisks in a server ? Rgds Nishant (4 Replies)
Discussion started by: thevishy
4 Replies

6. UNIX for Dummies Questions & Answers

Including case insensitivity into loop command

I have the following piece of code: for file in *csv; do cat $file >> $newfile; done for file in *CSV; do cat $file >> $newfile; done and would like to know how to combine these two rules into one? The combined rule would then include the case insensitivity that I am looking for. (4 Replies)
Discussion started by: figaro
4 Replies

7. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

8. Shell Programming and Scripting

Command to get File Timestamp including seconds [Aix 5.3]

Is there a command (like ls -l) to get the file time stamp including seconds? The ls -l gives only the HH:MM, No SS I don't have a C compiler to call stat() I don't a command like stat too. Please help. (8 Replies)
Discussion started by: firdousamir
8 Replies

9. UNIX for Advanced & Expert Users

whats command for search including sub directory ?

What is the command for search the program name from the entire directory (all directories in the unix box, means including sub directories...) (2 Replies)
Discussion started by: gksenthilkumar
2 Replies

10. Shell Programming and Scripting

Reading a path (including ref to shell variable) from file

Hi! 1. I have a parameter file containing path to log files. For this example both paths are the same, one is stated directly and the second using env variables. /oracle/admin/orcl/bdump/:atlas:trc:N ${ORACLE_BASE}/admin/${ORACLE_SID}/bdump/:${ORACLE_SID}:trc:N 2. I try to parse the path... (1 Reply)
Discussion started by: lojzev
1 Replies
Login or Register to Ask a Question