Logic Explanation of Match command in Linux


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Logic Explanation of Match command in Linux
# 1  
Old 03-07-2012
Logic Explanation of Match command in Linux

I am debugging a script and have stuck up at one code line
Code:
awk -F , '{if (match($3,001)) { print $2 } }' Master20120307090511.tmp

Code:
The Master20120307090511.tmp is
 
001,ARE , 001
002,ARE , 002
003,ARE , 003
006,ARE , 006
011,ARE , 011
012,ARE , 012

What happens is when i fire this
Code:
awk -F , '{if (match($3,001)) { print $2 } }' Master20120307090511.tmp
 
it outputs me as 
ARE
ARE
ARE

But if this command is fired
Code:
awk -F , '{if (match($3,011)) { print $2 } }' Master20120307090511.tmp
it returns me blank

I was wondering if 011 exists in the Master file then y does it return me blank

Could you all please help me in understanding this logic.

Thanks
# 2  
Old 03-07-2012
The reason for three matches to the first is that an unquoted integer in awk begining with zero is read as an octal number, thus 1 => 1 which occurs in three records and 11 => 9 (which isn't present).

try quoting instead to avoid this awk -F , '{if (match($3,"011")) { print $2 } }'
This User Gave Thanks to Skrynesaver For This Post:
# 3  
Old 03-07-2012
I would have never known this thanks a lot.
# 4  
Old 03-07-2012
This behavior seems to be gawk-only. The other awks I tried display ARE just fine without quotes.
If I use
Code:
awk --posix -F, '$3~011{ print $2 }' file

or
Code:
awk --posix -F , '{if (match($3,011)) { print $2 } }'

then ARE is displayed as well..

Perhaps we should introduce a standard banner on this forum:
if on Solaris use /usr/xpg4/bin/awk. When using gawk, use (g)awk --posix

Last edited by Scrutinizer; 03-07-2012 at 08:44 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 03-07-2012
Hi Skrynesaver

So now if i have
Code:
awk -F , '{if (match($3,014)) { print $2 } }' Master20120307090511.tmp
The decimal is 33

But 33 does not exists in the master file still it returns me

ARE

Is it because it considers only the number 3

Thanks
# 6  
Old 03-07-2012
014 octal = 12 decimal and there does appear to be a row with a 12 in $3 in the sample data provided...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Explanation of Nawk command

Hi Folks, I am struggling to understand nawk command which was used by another developer. Can you please explain what each character or string is doing here below: if ; then (3 Replies)
Discussion started by: kirans.229
3 Replies

2. UNIX for Dummies Questions & Answers

Explanation on problem "match" function awk

Hello Unix experts, If I could get any explanations on why the code below doesn't work it would be great ! My input looks like that ("|" delimited): Saaaaabbbbbccccc|ok Sdddddfffffggggg|ok The goal is, if $2 is "ok", to remove everything before the pattern given in the match function... (5 Replies)
Discussion started by: lucasvs
5 Replies

3. Shell Programming and Scripting

sed command explanation

Will someone give me an explanation on how the sed command below works. sed 's/.*//' Thanks! (3 Replies)
Discussion started by: scj2012
3 Replies

4. Shell Programming and Scripting

Perl scripts: requesting for logic explanation

Hi, balajesuri and durden_tyler, I have found your perl script for the thread https://www.unix.com/shell-programming-scripting/176370-perl-script-help-me-extracting-string.html, but find it difficult to understand the syntax. Could you or any forum members kindly shed some light on the logic... (3 Replies)
Discussion started by: royalibrahim
3 Replies

5. UNIX for Dummies Questions & Answers

Explanation of the sort command

Hi everyone, I am wondering if someone could please break down and explain the following sort command for me: ls ${DEST_LOCATION}/${FILES} | sort -rt -k 4,4n | head -1 I have tried working it out using 'man sort', but on AIX there is not a great explanation of this function. I know that... (9 Replies)
Discussion started by: jimbojames
9 Replies

6. Shell Programming and Scripting

Need explanation a of command in linux

Hi All I ran a script in Linux. In the script i have lines like && echo "Failed: Missing ${CM_ENV_FILE} \n" && return 1 . ${CM_ENV_FILE} Where CM_ENV_FILE = /data/ds/dpr_ebicm_uat//etl/cm3_0/entities/BBME/parameters/cm.env But its taking this path... (1 Reply)
Discussion started by: vee_789
1 Replies

7. UNIX for Advanced & Expert Users

command explanation

can anyone please tell me what does this expression means , i am under probation and need some explanation :) $AUDIT_DIR -type f -mtime +$AUDIT_EXPIRE \ -exec rm {} > /dev/null 2>&1 \; AUDIT_DIR="/var/log/" AUDIT_EXPIRE='30' Please use code tags! (4 Replies)
Discussion started by: semaan
4 Replies

8. UNIX for Dummies Questions & Answers

SED command explanation

can someone please explain the below sed command.. sed 's/\(*|\)\(.*\)/\2\1/' (6 Replies)
Discussion started by: raghu_shekar
6 Replies

9. Programming

Fuzzy Match Logic for Numerical Values

I have searched the internet (including these forums) and perhaps I'm not using the right wording. What I'm looking for is a function (preferably C) that analyzes the similitude of two numerical or near-numerical values, and returns either a true/false (match/nomatch) or a return code that... (4 Replies)
Discussion started by: marcus121
4 Replies

10. Shell Programming and Scripting

command line explanation

Hello everyone, I found this command line in a website: perl -pi.bak -we's/\z/Your new line\n/ if $. == 2;' your_text_file.txt With this command line you can insert a new line anywhere you want in a text without overwriting what's in it. -p causes perl to assume a loop around your... (4 Replies)
Discussion started by: goude
4 Replies
Login or Register to Ask a Question