how to sort inside awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to sort inside awk
# 1  
Old 09-18-2012
how to sort inside awk

Hi guys
I have a problem trying to sort output produced with the help of 'Awk'.
After "grepping" the pattern out of the file and sorting it, sort command acts a little strange:
Code:
$ grep -w -n -i "p[0-9]*cmo" /bb/data/rmt4db.lrl | sort -r
32:P1096CMO 63836 344 passthru
31:P1084CMO 121335 329 passthru
30:P102CMO 27889 115 passthru
29:P101CMO 28371 336 passthru
250:P290CMO 27052 322 passthru
249:P2907CMO 28409 334 passthru
247:P2887CMO 35028 287 passthru

note that all of the sudden line 250 is in the middle of the file when in fact it should be first followed by 249 and 247. 32 should be after them.
Is there a way to sort the file by the first field inside the 'awk' command?

The file is generated in accending order by the above command if "sort -r" is not used. May be someone knows how how to read the output from the buttom them. I work in ksh....

Thanks a lot for any advice....
# 2  
Old 09-18-2012
You want:
Code:
sort -rn

# 3  
Old 09-18-2012
Code:
... | sort -rn

should do the trick.
# 4  
Old 09-18-2012
Thanks a lot. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

2. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

3. Shell Programming and Scripting

Using variable inside awk

Hi, Please help me how to use variables inside awk in code below: ll | awk -v "yr=`date '+%Y'`" -v "mnth=`date '+%m'`" -v Jan=1 -v Feb=2 -v Mar=3 -v Apr=4 -v May=5 -v Jun=6 -v Jul=7 -v Aug=8 ' !/^d/ { if(NR>1) {printf "%-29s\t\t%s\t%5s\t\t%s %s,", $9,$1,$5,$`$6`,$7} }' Thanks. (10 Replies)
Discussion started by: manubatham20
10 Replies

4. Shell Programming and Scripting

use backtick inside awk

Hello, Can't we use backtick operator inside awk. nawk ' BEGIN { RS=""; FS="\</input\>" } { for(i=1;i<=NF;i++) { if ($i~/\"\"/) { print `grep XYZ $i`; print $i } } } ' test In the following code, I need to print $i and some... (8 Replies)
Discussion started by: shekhar2010us
8 Replies

5. Shell Programming and Scripting

AWK inside For loop

Hi, awk -F"|" 'BEGIN{sum=0}{sum+=$2}END{printf("%d\n", sum)}' css.txt awk -F"|" 'BEGIN{sum=0}{sum+=$3}END{printf("%d\n", sum)}' css.txt awk -F"|" 'BEGIN{sum=0}{sum+=$4}END{printf("%d\n", sum)}' css.txt awk -F"|" 'BEGIN{sum=0}{sum+=$5}END{printf("%d\n", sum)}' css.txt awk -F"|"... (3 Replies)
Discussion started by: thulasidharan2k
3 Replies

6. Shell Programming and Scripting

awk inside another awk statement

hi all, i have two files 1) a.txt one two three 2) abc "one" = 10 pqr "three" = 20 345 "two" = 0 this is what i want in third file (3 Replies)
Discussion started by: shishirkotkar
3 Replies

7. UNIX for Dummies Questions & Answers

Awk inside Awk expression

Hi, It can be used awk inside other Awk?. I need to get another text processing while other text process. Thank you. (2 Replies)
Discussion started by: pepeli30
2 Replies

8. Shell Programming and Scripting

How to sort values inside one column?

Hi, Could someone please help me with this? I have a text file that has fields seperated by comma. The last column in it has multiple values seperated by "|". I need to sort values in the last column seperated by pipe..is there any way I can do this through script? Sample text file - ... (7 Replies)
Discussion started by: sncoupons
7 Replies

9. Shell Programming and Scripting

control is not going inside the awk ?

Hi the control is not going inside the awk.. /u/bin/psg -t "pts/tC" |/bin/grep -e "ServTest" | /bin/grep -v "root" run separately in the command line lists the running process.. why is the code inside the awk not being being executed? can anyone tell what went wrong. ... (6 Replies)
Discussion started by: Anteus
6 Replies

10. Shell Programming and Scripting

using array inside awk

Hi All, I have the following code sequence for reading some bulk file and moving the content to two different arrays. while read data do THREEG_PATTERN=`echo $data | egrep "3G"` if then NEW_THREEG_PATTERN=`echo $THREEG_PATTERN | cut -d " " -f2` ... (12 Replies)
Discussion started by: subin_bala
12 Replies
Login or Register to Ask a Question