Meaning of awk script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Meaning of awk script
# 1  
Old 03-15-2014
Meaning of awk script

what does this mean?
Code:
awk '!a[$2]||a[$2]>$1 {a[$2]=$1} END {for (i in a) print a[i],i}' file

# 2  
Old 03-15-2014
I think it is supposed to list all lines where field 1 is the lowest for all lines that have an identical field 2...

But there is a bug and it should be:
Code:
awk '!($2 in a)||a[$2]>$1 {a[$2]=$1} END{for (i in a) print a[i],i}'

# 3  
Old 03-15-2014
1/ awk '!a[$2]||a[$2]>$1 {a[$2]=$1} END {for (i in a) print a[i],i}' file 2/ awk '!($2 in a)|

I try both of them but they omit some rows of input file thanks
# 4  
Old 03-15-2014
Yes they are supposed to omit certain rows, but one of them has a bug (is not working correctly)..
# 5  
Old 03-17-2014
list all lines where field 1 is the lowest for all lines that have an identical field 2

I have this file
Code:
0.450908 3 
0.491995 43 
0.492116 43 
0.493203 43 
0.493324 43 
0.494411 43 
0.494532 43 
0.495619 43 
0.49574 43 
0.509831 103 
0.535661 5 
0.535782 5 
0.536869 5 
0.53699 5 
0.538077 5 
0.538198 5 
0.538493 3 
0.538614 3 
0.539285 5 
0.539406 5 
0.539701 3 
0.539822 3 
0.540909 3 
0.54103 3 
0.542117 3 
0.542238 3 
0.543325 3 
0.543446 3 
0.544533 3 
0.544654 3 
0.545741 3 
0.545862 3 
0.546949 3 
0.54707 3 
0.548157 3

and i need the output file to be like this
Code:
0.450908 3
0.491995 43
0.535661 5 
0.509831 103

and i have those code

1/
Code:
awk '!a[$2]||a[$2]>$1 {a[$2]=$1} END {for (i in a) print a[i],i}' file

2/
Code:
awk '!($2 in a)||a[$2]>$1 {a[$2]=$1} END{for (i in a) print a[i],i}'

but both of them omit some rows of input file

Last edited by Scrutinizer; 03-17-2014 at 02:56 PM.. Reason: code tags
# 6  
Old 03-17-2014
Applying either of the awk scripts to your input file yields the desired output, albeit in a different sequence.
# 7  
Old 03-17-2014
I field 2 will never be zero, the bug will not come into play and there should not be a different output, between the 2 scripts.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Meaning of =~ in shell script

Please let me understand the meaning of following line in unix bash scripting .is =~ means not equal to or equal to . if ]; then echo -e "pmcmd startworkflow -sv ${INTSERV} -d ${INFA_DEFAULT_DOMAIN} -uv INFA_DEFAULT_DOMAIN_USER" \ "-pv INFA_DEFAULT_DOMAIN_PASSWORD -usdv... (2 Replies)
Discussion started by: harry00514
2 Replies

2. Shell Programming and Scripting

What is the meaning of ## in UNIX shell script?

Hi All, I am new to unix shell scripting and I was documenting one of the unix script and encountered below statements - for ii in `ls -1rt /oracle/admin/MARSCOPY/ext_files/fpm-ifpm/*.small.txt | tail -1 | awk '{print $1}'` do smallssim=${ii##/oracle/admin/MARSCOPY/ext_files/fpm-ifpm/}... (2 Replies)
Discussion started by: shuklajayb4
2 Replies

3. UNIX for Dummies Questions & Answers

UNIX Script - snipet meaning?

What would the below code snippet mean? my ($_configParam, $_paramValue) = split(/\s*=\s*/, $_, 2); $configParamHash{$_configParam} = $_paramValue; (2 Replies)
Discussion started by: MaKha
2 Replies

4. UNIX for Dummies Questions & Answers

Meaning of script with echo, -d, -f1, -f2

Hello Friends, I am a new learner of Unix & need to understand below script as start up, Can anyone explain the meaning of each line listed below. Thanks for your time. #!/usr/bin/ksh PARAMS=$1 #echo "parms passed is $PARAMS @" STATUS=`echo ${PARAMS} | cut -d: -f1` JOBNAME=`echo... (9 Replies)
Discussion started by: DK2014
9 Replies

5. UNIX for Dummies Questions & Answers

What is the meaning of this awk expression using sub?

Cannot understand what the "&" in the sub expression is supposed to do. {sub(/^/,"&" s)} (3 Replies)
Discussion started by: kristinu
3 Replies

6. Shell Programming and Scripting

Whats the meaning of set -e inside the script

Hi, I would like to ask about the meaning or purpose of set -e in the script bash, Does it mean if a wrong command in the script it will close or exit the script without continuation thats what happen if i set it in the terminal. Thanks in advance (3 Replies)
Discussion started by: jao_madn
3 Replies

7. UNIX for Dummies Questions & Answers

meaning of <<!

Hi all, I wanna know the meaning of the last word "<<! " sudo su - user <<! please help on this !!!! (1 Reply)
Discussion started by: sudharson
1 Replies

8. UNIX for Dummies Questions & Answers

meaning of script

hello every one i want to know meaning of following line INST_PARA=$HOME/install/Install.Para SAVEMEDIUM=`awk '$2=="ArchiveSave"{print$4}' $INST_PARA` (4 Replies)
Discussion started by: kaydream
4 Replies

9. Shell Programming and Scripting

Meaning of this line in script

Can anyone explain me the meaning of line #2 in these lines of shell script: if ; then ${EXPR} " ${MACTIONS } " : ".* ${ACTION} " >/dev/null 2>&1 || die "$USAGE" else Sorry in case this is a trivial thing (I am not an expert in this). (3 Replies)
Discussion started by: radiatejava
3 Replies

10. Shell Programming and Scripting

What is the meaning of "tab" in script ?

Hi, GOD bless you every body I have some script to compile a COBOL program - listed bellow - I have many questions about the syntax of this script: 1- There is many "tab" in the script, what does it means? Note: the tab is invisible, so I have colored it into red. 2- The "rm" command is... (3 Replies)
Discussion started by: so_friendly
3 Replies
Login or Register to Ask a Question