Sponsored Content
Full Discussion: Getline not working in awk
Top Forums Shell Programming and Scripting Getline not working in awk Post 302834331 by MadeInGermany on Thursday 18th of July 2013 05:35:02 PM
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:
 

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
SHTOOL-PATH.TMP(1)					      GNU Portable Shell Tool						SHTOOL-PATH.TMP(1)

NAME
shtool-path - GNU shtool command dealing with shell path variables SYNOPSIS
shtool path [-s|--suppress] [-r|--reverse] [-d|--dirname] [-b|--basename] [-m|--magic] [-p|--path path] str [str ...] DESCRIPTION
This command deals with shell $PATH variables. It can find a program through one or more filenames given by one or more str arguments. It prints the absolute filesystem path to the program displayed on "stdout" plus an exit code of 0 if it was really found. OPTIONS
The following command line options are available. -s, --suppress Supress output. Useful to only test whether a program exists with the help of the return code. -r, --reverse Transform a forward path to a subdirectory into a reverse path. -d, --dirname Output the directory name of str. -b, --basename Output the base name of str. -m, --magic Enable advanced magic search for ""perl"" and ""cpp"". -p, --path path Search in path. Default is to search in $PATH. EXAMPLE
# shell script awk=`shtool path -p "${PATH}:." gawk nawk awk` perl=`shtool path -m perl` cpp=`shtool path -m cpp` revpath=`shtool path -r path/to/subdir` HISTORY
The GNU shtool path command was originally written by Ralf S. Engelschall <rse@engelschall.com> in 1998 for Apache. It was later taken over into GNU shtool. SEE ALSO
shtool(1), which(1). 18-Jul-2008 shtool 2.0.8 SHTOOL-PATH.TMP(1)
All times are GMT -4. The time now is 11:59 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy