Sort with Awk, sed ....


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort with Awk, sed ....
# 1  
Old 10-26-2010
Sort with Awk, sed ....

I want to print out the lines that have the max value in column 3. and count the occurrence of column 1; if there are more than one occurrences, line with highest column 2 value will be printed.
I have this data:

input:

Code:
AV 234 25 
AV 256 76
AS 421 34
AV 124 76
BD 136 71
BD 241 76
AW 111 76
BD 241 76
BD 255 76

Output:

Code:
AV 256 76 2
BD 255 76 3
AW 111 76 1



---------- Post updated at 06:35 AM ---------- Previous update was at 05:14 AM ----------




Can someone please help with this?
# 2  
Old 10-26-2010
Hi, what have you tried so far?
# 3  
Old 10-26-2010
I know the first step is to sort out the Max value on column 3, i just dont know how to go about it.
# 4  
Old 10-26-2010
Once you know that maximum, you could then reparse the input. I came up with this:
Code:
awk 'NR==FNR&&$3>m{m=$3;next} $3==m&&$2>A[$1]{A[$1]=$2; B[$1]++; C[$1]=$0} END{for(i in A)print C[i], B[i]}' infile infile

# 5  
Old 10-26-2010
Code:
sort -nrk3 infile | awk 'NR==1{m=$3;a[$1]++;b[$1]=(b[$1])?b[$1]:$0;next}$3==m{a[$1]++;b[$1]=(b[$1])?b[$1]:$0}END{for (i in a){print b[i],a[i]}}'


Last edited by bartus11; 10-26-2010 at 10:17 AM..
# 6  
Old 10-26-2010
i'm not getting result
# 7  
Old 10-26-2010
What system are you using?
This User Gave Thanks to bartus11 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Wget, grep, sort, sed in 1 command/script

Hi, I need to join these statements for efficiency, and without having to make a new directory for each batch. I'm annotating commands below. wget -q -r -l1 URL ^^ can't use -O - here and pipe | to grep because of -r grep -hrio "\b\+@\+\.\{2,4\}\+\b" * > first.txt ^^ Need to grep the output... (14 Replies)
Discussion started by: p1ne
14 Replies

2. Shell Programming and Scripting

sed --> sort data by date

Hi, i "tried" to sort data by date. So far, i used sed to take the data from the last and the actual month. Now, after changing the year it is not working properly. i use: GNU bash, version 4.2.45(1)-release (x86_64-suse-linux-gnu) sed -n '/\//p' $Home/../scripte/pd_0.txt y is a... (6 Replies)
Discussion started by: IMPe
6 Replies

3. Shell Programming and Scripting

Want to sort a file using awk & sed to get required output

Hi All, Need Suggestion, Want to sort a file using awk & sed to get required, output as below, such that each LUN shows correct WWPN and FA port Numbers correctly: Required output: 01FB 10000000c97843a2 8C 0 01FB 10000000c96fb279 9C 0 22AF 10000000c97843a2 8C 0 22AF 10000000c975adbd ... (10 Replies)
Discussion started by: aix_admin_007
10 Replies

4. Shell Programming and Scripting

awk sort

input file abc1 abc23 abc12 abc15 output abc1 abc12 abc15 abc23 (9 Replies)
Discussion started by: yanglei_fage
9 Replies

5. Shell Programming and Scripting

Remove duplicate chars and sort string [SED]

Hi, INPUT: DCBADD OUTPUT: ABCD The SED script should alphabetically sort the chars in the string and remove the duplicate chars. (5 Replies)
Discussion started by: jds93
5 Replies

6. Shell Programming and Scripting

Using a combination of sort/cut/grep/awk/join/paste/sed

I have a file and need to only select users that have a shell of “/bin/bash” in the line using awk or sed please help (4 Replies)
Discussion started by: boyboy1212
4 Replies

7. Shell Programming and Scripting

2 problems... sed and sort

Hi everyone! I have a file like that: And I would it like that: I don't know how to keep the first field and sort the second one. I have a second question with sed... to put the variable $VAR at the beginning of the file... But I have an output like this: snork... (3 Replies)
Discussion started by: Castelior
3 Replies

8. Shell Programming and Scripting

Help, awk sed sort

Hi,everyone: I'm new to shell, and now get trouble with some script: line=`/usr/xpg4/bin/awk '/^(*\|){2}'"$CR"'/ {print $0}' ${TIER4FILE}|grep -v OSNAME=`sed -n ''$LINE'p' $DATALOC/os` sort +1 /tmp/LhasaCRs2 > /tmp/LhasaCRs1 I cannot understand the "{2}" here. My mentor said it... (1 Reply)
Discussion started by: mycoy
1 Replies

9. Shell Programming and Scripting

Sed with sort doesnt work

Sed with sort doesnt work The below code doesnt work: sed -e '/^$/d' -e 's/,/|/g' | sort -t"|" -k1,1 -u file1 when i seperate them it work but i have to create intermediate file which i dont want to: sed -e '/^$/d' -e 's/,/|/g' file1 > file2 sort -t"|" -k1,1 -u file2 Help... (2 Replies)
Discussion started by: pinnacle
2 Replies

10. Shell Programming and Scripting

how to use sed to sort out this question

root:x:0:0:0000-Admin(0000):/:/sbin/sh rootcsh:x:0:1:0000-Admin(0000):/:/bin/csh nocol:x:6312:630:NOCOL Monitor,,,:/usr/local/lib/nocol-client:/usr/local/bin/bash nobody:x:60001:60001:uid no body:/:/usr/local/bin/bash noaccess:x:60002:60002:uid no access:/:/usr/local/bin/bash... (2 Replies)
Discussion started by: sonicstage
2 Replies
Login or Register to Ask a Question