Explanation on problem "match" function awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Explanation on problem "match" function awk
# 1  
Old 05-25-2013
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):
Code:
Saaaaabbbbbccccc|ok
Sdddddfffffggggg|ok

The goal is, if $2 is "ok", to remove everything before the pattern given in the match function below. Otherwise keep the original string in $1:
Code:
gawk -F'|'
'{
    if($2 ~ /^ok$/){
        match($1,/(bbbbb)/,f)
	split($1,array,f[1])
	sub(array[1],"",$1)
	}
	
    else{
        $1 == $1
	}
		
	print $1 OFS $2	
}

in order to get:
Code:
bbbbbccccc|ok
Sdddddfffffggggg|ok

But when the record matches $2 but not the regex inside "match", I don't understand why it removes the first character:
Code:
bbbbbccccc|ok
dddddfffffggggg|ok

If someone could explain that to me please.

Thanks guys !

Last edited by lucasvs; 05-25-2013 at 05:16 AM..
# 2  
Old 05-25-2013
This is confusing - please give us expected output based on the input you gave.
# 3  
Old 05-25-2013
As far as I know awk match function syntax is:
Code:
match (string, regexp)

I noticed that your are passing 3 arguments for match function which is wrong!

To get your expected output, you could simply do:
Code:
gawk -F'|' '
        {
                if ( $2 == "ok" )
                {
                        n = match ( $1, /bbbbb.*/ )
                        if ( n )
                        {
                                $1 = substr ( $1, RSTART, RLENGTH )
                        }
                }
        }
        1
' OFS='|' file

# 4  
Old 05-25-2013
Quote:
Originally Posted by Yoda
As far as I know awk match function syntax is:
Code:
match (string, regexp)

I noticed that your are passing 3 arguments for match function which is wrong!
That is incorrect. Check manual page for gawk.

@OP:
Check this:
Code:
gawk 'BEGIN{
field1="Sdddddfffffggggg"
split(field1,array,"")
print "Pattern : [" array[1] "]"
print "Field 1 :[" field1 "]"
sub(array[1],"",field1)
print "Field 1 after substitution: [" field1 "]" }'
 
Pattern : [S]
Field 1 :[Sdddddfffffggggg]
Field 1 after substitution: [dddddfffffggggg]

Do you get it? If no, check the explanation of match function in gawk's man page (specifically the manner in which the 3rd argument (array) is populated).

Last edited by elixir_sinari; 05-25-2013 at 03:29 PM..
This User Gave Thanks to elixir_sinari For This Post:
# 5  
Old 05-25-2013
My bad. GNU awk does supports an optional 3rd parameter!
Code:
match (string, regex [, array])

# 6  
Old 05-25-2013
Hmmmm - the requestor didn't ask for alternative solutions, he asked to explain why that "strange" behaviour occurred (which, btw, is not strange but correct). Let's go and try: According to the gawk man page, the array f will be filled with "the matches". The first match ( f[1] ) is being used as a separator in the following split, and then the chars before the first match (array[1]) will be substituted with "".
If there's no match, like in the second line, f will be empty, and split will use an empty separator, resulting in a per char split. array[1] will then hold the "S" which will be removed.
Logical, understandable?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search file containing ps results for a match "my.cnf" and then for a second match . "ok:" and

I need to find two matches in the output from ps. I am searching with ps -ef |grep mysql for: my.cnf /bin/sh /usr/bin/mysqld_safe --defaults-file=/data/mysql/master/agis_core/etc/my.cnf after this match I want to search back and match the hostname which is x number of lines back, above the... (2 Replies)
Discussion started by: bash_in_my_head
2 Replies

2. UNIX for Advanced & Expert Users

AIX - io info get from "libperfstat" not match "iostat"

Hi, everyone. I need to write a program to get io info based on libperfstat. But the "write time" of a disk is just half of the value get from iostat. I'm confused and can't explain. Help please. How I calculate "write service time per sec": In iostat: write service... (0 Replies)
Discussion started by: jackliang
0 Replies

3. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. UNIX for Dummies Questions & Answers

Explanation of "total" field in "ls -l" command output

When I do a listing in one particular directory (ls -al) I get: total 43456 drwxrwxrwx 2 root root 4096 drwxrwxrwx 3 root root 4096 -rwxrwxr-x 1 nobody nobody 3701594 -rwxrwxr-x 1 nobody nobody 3108510 -rwxrwxr-x 1 nobody nobody 3070580 -rwxrwxr-x 1 nobody nobody 3099733 -rwxrwxr-x 1... (1 Reply)
Discussion started by: proactiveaditya
1 Replies

6. Shell Programming and Scripting

awk statement to match all lines starting with "#"

Looking for awk statement that will match all lines starting with "# " if ( $1 == \^"#" ) Input file: # of the server. If you would like to set these, please take out the # pound (#) sign in front of one or all severities and set it equal to # severity desired. For example, FATAL=3 #... (2 Replies)
Discussion started by: Arsenalman
2 Replies

7. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

8. HP-UX

ERROR: more than one instance of overloaded function "vprintf" has "C" linkage

Hi people! I've got this own library: -------------------------------------------- Personal.h -------------------------------------------- #ifdef __cplusplus extern "C" { #endif #include <stdio.h> #include <stdarg.h> #include <string.h> ... (0 Replies)
Discussion started by: donatoll
0 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

10. Programming

How to convert the "select" function into a "poll" function

i have a program using the select function but i want to convert it to poll... how can i do this? thanks in advance... :) (1 Reply)
Discussion started by: rbolante
1 Replies
Login or Register to Ask a Question