Sponsored Content
Full Discussion: Getline not working in awk
Top Forums Shell Programming and Scripting Getline not working in awk Post 302833477 by siramitsharma on Wednesday 17th of July 2013 07:49:18 AM
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
                                }
}'

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 08:01 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy