Getline not working in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getline not working in awk
# 1  
Old 07-17-2013
Getline not working in awk

hi,
i am trying to parse a file in awk to generate a output to be written in a file depeding upon some condition. below is the code

Content of file1
Code:
919873741577,9131638459976206,20130715150109,S,919811000214,2A65,405899136999995
91225,9132019667696305,20130715150110,S,USSDLIKE,161A,405899136999995
54321,9132008640045585,20130715150109,S,SYMBIOTIC,2470,405899136999995
54321,9132008640041216,20130715150108,S,SYMBIOTIC,2458,405899136999995
54321,9132008640045246,20130715150109,S,SYMBIOTIC,2470,405899136999995
919865754417,9194*665665,20130715161714,F,2374,,919875089998
918926048523,911401727253,20130715141326,F,405899153999998,,919875089998
918640868045,911401727253,20130715165419,F,405899153999999,,919875089998

Code for parsing is as follows
Code:
 awk -F, -v p2pcdr="file1" -v p2preject="P2P_REJ.txt"
                        ' BEGIN{
                                while ((getline line < p2pcdr ) > 0 )
                                {
                                        if ( $2 ~ "[*,#]" || ( ($2 + 0) !~ "^[0,5,7,8,9]")  || length($2 + 0) <4 || ($2 !~ "^[0,5,7,8,9]"))
                                                P2PREJECTCDR[line]
                                        else if ( (length($6) == 13 || length($6) == 14 || length($6)==11) && !($6 ~ "^[A-Z,a-z]") )
                                                P2PREJECTCDR[line]
                                        else if ($6 =="" && substr($2,1,4)=="0091" && substr($2 + 0,3,1) !~ "^[1,2,5,7,8,9]")
                                                P2PREJECTCDR[line]
                                        else if ( $6 == "" && substr($2,1,2) == "00" && length($2) == "12" )
                                                P2PCDR[$1,$2,$3,$4,$5,$2 + 0,$7]
                                        else if ( $6 == "" && substr($2,1,1) == "0" && length($2) == "11" )
                                                P2PCDR[$1,$2,$3,$4,$5,$2 + 0,$7]
                                        else if ( $6 == "" && substr($2,1,3) == "+91" && length($2) == "13" )
                                                P2PCDR[$1,$2,$3,$4,$5,substr($2,2,12),$7]
                                        else if ( $6 == "" && substr($2,1,2) == "91" && length($2) == "12" && (substr($2,3,1) ~ "^[5,7,8,9]") )
                                                P2PCDR[$1,$2,$3,$4,$5,$2,$7]
                                        else if ($5 != "" && $6 != "")
                                                P2PCDR[line]
                                        else if ($5 == "" && $6 != "")
                                                P2PCDR[line]
                                        else if ($6 == "" && length($2)<=10 && length($2 + 0) >= 4 )
                                                P2PCDR[$1,$2,$3,$4,$5,$2 + 0,$7]
                                        else
                                                P2PREJECTCDR[line]
                                } close (p2pcdr)
                             }   
                                 END
                                {
                                        for ( counter in P2PCDR )
                                                print counter >> "P2P.txt"
                                        for ( counter in P2PREJECTCDR )
                                                print counter >> p2preject
                                }
                        '

can you please help as this is not working
# 2  
Old 07-17-2013
There is a fundamental mis-understanding how awk works.
Your code reads an entire file in the BEGIN section,
and then skips the main section with the built-in loop over the file(s) given in the awk arguments.
# 3  
Old 07-17-2013
yes, there is Smilie .
But can you please suggest how to get this thing rectified.
# 4  
Old 07-17-2013
Do you know the joke "could you heal this cow", showing the doctor a steak?
Please give more information!
What is the role of the 2nd file?
What is the expected outcome?
# 5  
Old 07-17-2013
if you could see the input file, first 5 lines will be valid as per the conditions mentioned in the begin block & that is the reason it is being redirected to P2P.txt in the END block, whereas last 3 lines of the input file will be redirected to the invalid data & this invalid data will be pushed to other file "P2P_REJ.txt"


inputfile file1(is comma separated)
Code:
919873741577,9131638459976206,20130715150109,S,919811000214,2A65,405899136999995
91225,9132019667696305,20130715150110,S,USSDLIKE,161A,405899136999995
54321,9132008640045585,20130715150109,S,SYMBIOTIC,2470,405899136999995
54321,9132008640041216,20130715150108,S,SYMBIOTIC,2458,405899136999995
54321,9132008640045246,20130715150109,S,SYMBIOTIC,2470,405899136999995
919865754417,9194*665665,20130715161714,F,2374,,919875089998
918926048523,911401727253,20130715141326,F,405899153999998,,919875089998
918640868045,911401727253,20130715165419,F,405899153999999,,919875089998

1st Output required based on conditions in P2P.txt
Code:
919873741577,9131638459976206,20130715150109,S,919811000214,2A65,405899136999995
91225,9132019667696305,20130715150110,S,USSDLIKE,161A,405899136999995
54321,9132008640045585,20130715150109,S,SYMBIOTIC,2470,405899136999995
54321,9132008640041216,20130715150108,S,SYMBIOTIC,2458,405899136999995
54321,9132008640045246,20130715150109,S,SYMBIOTIC,2470,405899136999995

2nd Output required based on conditions in P2P_REJ.txt would contain 3 lines based from input file
Code:
919865754417,9194*665665,20130715161714,F,2374,,919875089998
918926048523,911401727253,20130715141326,F,405899153999998,,919875089998
918640868045,911401727253,20130715165419,F,405899153999999,,919875089998

writing the actual code again for reference
Code:
awk -F "," -v p2pcdr="file1" -v p2preject="P2P_REJ.txt" '
BEGIN {
		while ((getline line<p2pcdr ) > 0 )
		{
			if ( $2 ~ "[*,#]" || ( ($2 + 0) !~ "^[0,5,7,8,9]")  || length($2 + 0) <4 || ($2 !~ "^[0,5,7,8,9]"))
                                                P2PREJECTCDR[line]
			else if ( (length($6) == 13 || length($6) == 14 || length($6)==11) && !($6 ~ "^[A-Z,a-z]") )
                                                P2PREJECTCDR[line]
			else if ($6 =="" && substr($2,1,4)=="0091" && substr($2 + 0,3,1) !~ "^[1,2,5,7,8,9]")
                                                P2PREJECTCDR[line]
                                       	else if ($6 =="" && substr($2,1,2)=="91" && length($2) !=12 && length($2) !=16 && substr($2,3,1) !~ "^[5]")
                                                P2PREJECTCDR[line]
                                        else if ( substr($2,1,2)=="91" && (length(substr($2,3,length($2)-2) ) < 4) )
                                                P2PREJECTCDR[line]
                                        else if ( substr($2,1,4)=="0091" && (length(substr($2+0,3,length($2)-2) ) < 4) )
                                                P2PREJECTCDR[line]
                                        else if ($6!="" && length($6)>10 && $6 ~ "^[1,2,3,4,6,7,8]")
                                                P2PREJECTCDR[line]
                                        else if ( $6!="" && length($6)!=10 && length($6)!=12 && length($6)!=4 && length($6)!=6 && $6!~"^[A-Z,a-z]" && substr($6,1,2)!="91" )
                                                P2PREJECTCDR[line]
                                        else if ($6!="" && length($6)==10 && $6 ~ "^[1,2,4,6]")
                                                P2PREJECTCDR[line]
                                        else if ( $6 == "" && substr($2,1,4) == "0091" )
                                                P2PCDR[$1,$2,$3,$4,$5,$2 + 0,$7]
                                        else if ( $6 == "" && substr($2,1,2) == "91" && length($2) == "16" )
                                                P2PCDR[$1,$2,$3,$4,$5,substr($2,3,4),$7]
                                        else if ( $6 == "" && substr($2,1,2) == "00" && length($2) == "12" )
                                                P2PCDR[$1,$2,$3,$4,$5,$2 + 0,$7]
                                        else if ( $6 == "" && substr($2,1,1) == "0" && length($2) == "11" )
                                                P2PCDR[$1,$2,$3,$4,$5,$2 + 0,$7]
                                        else if ( $6 == "" && substr($2,1,3) == "+91" && length($2) == "13" )
                                                P2PCDR[$1,$2,$3,$4,$5,substr($2,2,12),$7]
                                        else if ( $6 == "" && substr($2,1,2) == "91" && length($2) == "12" && (substr($2,3,1) ~ "^[5,7,8,9]") )
                                                P2PCDR[$1,$2,$3,$4,$5,$2,$7]
                                        else if ($5 != "" && $6 != "")
                                                P2PCDR[line]
                                        else if ($5 == "" && $6 != "")
                                                P2PCDR[line]
                                        else if ($6 == "" && length($2)<=10 && length($2 + 0) >= 4 )
                                                P2PCDR[$1,$2,$3,$4,$5,$2 + 0,$7]
                                        else
                                                P2PREJECTCDR[line]
		} close (p2pcdr)
	 END
                                {
                                        for ( counter in P2PCDR )
                                                print counter >> "P2P.txt"
                                        for ( counter in P2PREJECTCDR )
                                                print counter >> p2preject
                                }
}'

# 6  
Old 07-18-2013
Like I said, replace the BEGIN by a main loop over the first file argument (file1).
The variable line becomes $0, and $1,$2 etc. are the fields of $0.
Further, awk does not accept if ... else if ... without deeper { nesting }. (This is unlike shell and C.)
I have replaced with next that directly jumps to the next cycle (next line).
I further have replaced the outer { braces } that allows to use implicit if.
Also awk does not accept a new line everywhere. Here is my result:
Code:
awk -F, -v p2preject="P2P_REJ.txt" '
  $2 ~ "[*,#]" || ( ($2 + 0) !~ "^[0,5,7,8,9]")  || length($2 + 0) <4 || ($2 !~ "^[0,5,7,8,9]") { P2PREJECTCDR[$0]; next }
  (length($6) == 13 || length($6) == 14 || length($6)==11) && !($6 ~ "^[A-Z,a-z]") { P2PREJECTCDR[$0]; next }
  $6 =="" && substr($2,1,4)=="0091" && substr($2 + 0,3,1) !~ "^[1,2,5,7,8,9]" { P2PREJECTCDR[$0]; next }
  $6 == "" && substr($2,1,2) == "00" && length($2) == "12" { P2PCDR[$1,$2,$3,$4,$5,$2 + 0,$7]; next }
  $6 == "" && substr($2,1,1) == "0" && length($2) == "11" { P2PCDR[$1,$2,$3,$4,$5,$2 + 0,$7]; next }
  $6 == "" && substr($2,1,3) == "+91" && length($2) == "13" { P2PCDR[$1,$2,$3,$4,$5,substr($2,2,12),$7]; next }
  $6 == "" && substr($2,1,2) == "91" && length($2) == "12" && (substr($2,3,1) ~ "^[5,7,8,9]") { P2PCDR[$1,$2,$3,$4,$5,$2,$7]; next }
  $5 != "" && $6 != "" { P2PCDR[$0]; next }
  $5 == "" && $6 != "" { P2PCDR[$0]; next }
  $6 == "" && length($2)<=10 && length($2 + 0) >= 4 { P2PCDR[$1,$2,$3,$4,$5,$2 + 0,$7]; next }
  { P2PREJECTCDR[$0] }
  END {
    for ( counter in P2PCDR ) print counter >> "P2P.txt"
    for ( counter in P2PREJECTCDR ) print counter >> p2preject
  }
' file1

This User Gave Thanks to MadeInGermany For This Post:
# 7  
Old 07-19-2013
Thanks made in germany
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk with if, getline, and another if

Howdy Folks, It seems like it is always awk that confuses the heck out of me and I even have books and examples. I have this line: awk '{if (/clientIP/)(SRV = $NF); if ($2 ~ /BUNDLE-GIM/) getline; if ($2 ~ /r100595/) {print SRV,"BUNDLE-GIM",$2}}' post.txt to parse this text: <api... (4 Replies)
Discussion started by: port43
4 Replies

2. Shell Programming and Scripting

awk getline

Hi, I have an awk script with the following function in it . function cmd( c ) { while( ( c | getline foo) > 0 ){ return foo ; close( c ); } } c =... (4 Replies)
Discussion started by: MetaMan
4 Replies

3. Shell Programming and Scripting

awk getline t file

I want to import a textfile with getline into var t which has several lines. How do import all lines, since it only imports the last line: while < ((getline t "textfile") > 0) (7 Replies)
Discussion started by: sdf
7 Replies

4. Shell Programming and Scripting

Some Awk Getline help?

Greetings, I have about 3000 files that I want to search. The first column in all of these 3000 files has a unique serial number on each line. The subsequent columns have lots of data. I have another masterfile with three columns to help me find all the data I need in a moments notice: col 1... (15 Replies)
Discussion started by: jeeplou
15 Replies

5. Shell Programming and Scripting

Using getline in awk

I am using awk and want to use getline from a file like below getline x < file However file consists of two columns and I only want to store $2 Any way I can do this? ---------- Post updated at 06:54 AM ---------- Previous update was at 06:45 AM ---------- Done something like this.... (1 Reply)
Discussion started by: kristinu
1 Replies

6. Shell Programming and Scripting

awk getline

How do you make the getline function return to the original line? The example below should make it clear where I am currently going wrong. Thanks AWK SCRIPT: ------------- awk -F '-' '{ tmpLine = "EMPTY" print "CURRENT LINE :"$0 getline tmpLine print "NEXT LINE :"tmpLine }'... (1 Reply)
Discussion started by: garethsays
1 Replies

7. Shell Programming and Scripting

awk and system getline

Hello, Need some help here. I have this script (test.sh): #!/bin/sh var=$1 (( var = 2 * var )) echo $var Now I want to call this script from awk with one argument and then capture the result in a variable, something like: echo 40 | awk ' { x = $1; "test.sh " x | getline y; print y }... (1 Reply)
Discussion started by: fbg
1 Replies

8. Shell Programming and Scripting

get sysname with getline in awk

I am having issues getting this working. I am trying to get the system name and print it out in a awk script. I looked and looked, it looks like my syntax is correct but it is not working. What am I overlooking?? BEGIN { sys1 = "system( "uname -n" )" "sys1" | getline sys ... (2 Replies)
Discussion started by: timj123
2 Replies

9. Shell Programming and Scripting

awk getline help maybe?

hello collegues, I am attempting to use awk to search file1 (serverlist.csv) from each row with file2 (supported.txt). If the is no entry exists in serverlist then output to a file called notsupp.out if there is an entry output to supp.out I can do this with basic shell scripting however... (0 Replies)
Discussion started by: chlawren
0 Replies

10. Shell Programming and Scripting

awk:Problem with getline

$ echo |awk ' BEGIN {"date" | getline current_time;close("date");print "Report printed on " current_time}' Report printed on Thu May 11 14:57:29 METDST 2006 This example works fine but how can i print all the output when is longer... (3 Replies)
Discussion started by: Klashxx
3 Replies
Login or Register to Ask a Question