awk with if, getline, and another if


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk with if, getline, and another if
# 1  
Old 02-27-2018
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:
Code:
awk '{if (/clientIP/)(SRV = $NF); if ($2 ~ /BUNDLE-GIM/) getline; if ($2 ~ /r100595/) {print SRV,"BUNDLE-GIM",$2}}' post.txt

to parse this text:
Code:
<api gim_list_client_modules clientIP=srv1001
ID=0
####### ENTRY 0 #######
MODULE_ID:          -1
NAME:               COMMON
INSTALLED_VERSION   100_r0_3
SCHEDULED_VERSION   100_r0_3
STATE:              INSTALLED
IS_SCHEDULED:       N
####### ENTRY 1 #######
MODULE_ID:          306
NAME:               GIM
INSTALLED_VERSION   10.1.4_r103106_1
SCHEDULED_VERSION   10.1.4_r103106_1
STATE:              INSTALLED
IS_SCHEDULED:       N
####### ENTRY 2 #######
MODULE_ID:          305
NAME:               SUPERVISOR
INSTALLED_VERSION   10.1.4_r103106_1
SCHEDULED_VERSION   10.1.4_r103106_1
STATE:              INSTALLED
IS_SCHEDULED:       N
####### ENTRY 3 #######
MODULE_ID:          303
NAME:               INIT
INSTALLED_VERSION   10.1.4_r103106_1
SCHEDULED_VERSION   10.1.4_r103106_1
STATE:              INSTALLED
IS_SCHEDULED:       N
####### ENTRY 4 #######
MODULE_ID:          304
NAME:               UTILS
INSTALLED_VERSION   10.1.4_r103106_1
SCHEDULED_VERSION   10.1.4_r103106_1
STATE:              INSTALLED
IS_SCHEDULED:       N
####### ENTRY 5 #######
MODULE_ID:          307
NAME:               BUNDLE-GIM
INSTALLED_VERSION   10.1.4_r103106_1
SCHEDULED_VERSION   10.1.4_r103106_1
STATE:              INSTALLED
IS_SCHEDULED:       N
####### ENTRY 6 #######
MODULE_ID:          340
NAME:               BUNDLE-STAP
INSTALLED_VERSION   10.1.4_r103106_1
SCHEDULED_VERSION   10.1.4_r103106_1
STATE:              INSTALLED
IS_SCHEDULED:       N
<api gim_list_client_modules clientIP=srv1002
ID=0
####### ENTRY 0 #######
MODULE_ID:          -1
NAME:               COMMON
INSTALLED_VERSION   10.1.2_r0_3
SCHEDULED_VERSION   10.1.2_r0_3
STATE:              INSTALLED
IS_SCHEDULED:       N
####### ENTRY 1 #######
MODULE_ID:          7
NAME:               GIM
INSTALLED_VERSION   10.1.2_r100595_1
SCHEDULED_VERSION   10.1.2_r100595_1
STATE:              INSTALLED
IS_SCHEDULED:       N
####### ENTRY 2 #######
MODULE_ID:          10
NAME:               SUPERVISOR
INSTALLED_VERSION   10.1.2_r100595_1
SCHEDULED_VERSION   10.1.2_r100595_1
STATE:              INSTALLED
IS_SCHEDULED:       N
####### ENTRY 3 #######
MODULE_ID:          8
NAME:               INIT
INSTALLED_VERSION   10.1.2_r100595_1
SCHEDULED_VERSION   10.1.2_r100595_1
STATE:              INSTALLED
IS_SCHEDULED:       N
####### ENTRY 4 #######
MODULE_ID:          9
NAME:               UTILS
INSTALLED_VERSION   10.1.2_r100595_1
SCHEDULED_VERSION   10.1.2_r100595_1
STATE:              INSTALLED
IS_SCHEDULED:       N
####### ENTRY 5 #######
MODULE_ID:          11
NAME:               BUNDLE-GIM
INSTALLED_VERSION   10.1.2_r100595_1
SCHEDULED_VERSION   10.1.2_r100595_1
STATE:              INSTALLED
IS_SCHEDULED:       N
<api gim_list_client_modules clientIP=srv1003
ID=0
####### ENTRY 0 #######
MODULE_ID:          -1
NAME:               COMMON
INSTALLED_VERSION   100_r0_3
SCHEDULED_VERSION   100_r0_3
STATE:              INSTALLED
IS_SCHEDULED:       N
####### ENTRY 1 #######
MODULE_ID:          101
NAME:               GIM
INSTALLED_VERSION   10.1.3_r101342_1
SCHEDULED_VERSION   10.1.3_r101342_1
STATE:              INSTALLED
IS_SCHEDULED:       N
####### ENTRY 2 #######
MODULE_ID:          100
NAME:               SUPERVISOR
INSTALLED_VERSION   10.1.3_r101342_1
SCHEDULED_VERSION   10.1.3_r101342_1
STATE:              INSTALLED
IS_SCHEDULED:       N
####### ENTRY 3 #######
MODULE_ID:          98
NAME:               INIT
INSTALLED_VERSION   10.1.3_r101342_1
SCHEDULED_VERSION   10.1.3_r101342_1
STATE:              INSTALLED
IS_SCHEDULED:       N
####### ENTRY 4 #######
MODULE_ID:          99
NAME:               UTILS
INSTALLED_VERSION   10.1.3_r101342_1
SCHEDULED_VERSION   10.1.3_r101342_1
STATE:              INSTALLED
IS_SCHEDULED:       N
####### ENTRY 5 #######
MODULE_ID:          102
NAME:               BUNDLE-GIM
INSTALLED_VERSION   10.1.3_r101342_1
SCHEDULED_VERSION   10.1.3_r101342_1
STATE:              INSTALLED
IS_SCHEDULED:       N

and my results are:
Code:
clientIP=srv1002 BUNDLE-GIM 10.1.2_r100595_1
clientIP=srv1002 BUNDLE-GIM 10.1.2_r100595_1
clientIP=srv1002 BUNDLE-GIM 10.1.2_r100595_1
clientIP=srv1002 BUNDLE-GIM 10.1.2_r100595_1
clientIP=srv1002 BUNDLE-GIM 10.1.2_r100595_1
clientIP=srv1002 BUNDLE-GIM 10.1.2_r100595_1
clientIP=srv1002 BUNDLE-GIM 10.1.2_r100595_1
clientIP=srv1002 BUNDLE-GIM 10.1.2_r100595_1
clientIP=srv1002 BUNDLE-GIM 10.1.2_r100595_1
clientIP=srv1002 BUNDLE-GIM 10.1.2_r100595_1

What I really want is to add an && so that I only print when $2 contains BUNDLE-GIM and $2 of the very next line contains r100595 but I cannot figure out how to do it.

Thanks for your attention,
T
# 2  
Old 02-27-2018
Try
Code:
awk '{if (/clientIP/)(SRV = $NF); if ($2 ~ /BUNDLE-GIM/) {getline; G=1}; if (G && $2 ~ /r100595/) print SRV,"BUNDLE-GIM",$2; G=0}' file
clientIP=srv1002 BUNDLE-GIM 10.1.2_r100595_1

EDIT: or, to improve readability
Code:
awk '
/clientIP/              {SRV = $NF
                        }
$2 ~ /BUNDLE-GIM/       {getline
                         if ($2 ~ /r100595/) print SRV,"BUNDLE-GIM",$2
                        }
' file

Please be aware that this assumes that there always will be a line to get so error handling has been skipped.

Last edited by RudiC; 02-27-2018 at 12:59 PM..
This User Gave Thanks to RudiC For This Post:
# 3  
Old 02-27-2018
Thank you, Rudi!
# 4  
Old 02-27-2018
Alternative approaches you could try:
Code:
awk '
  {
    for(i=9; i<=NF; i++)
      if($i=="BUNDLE-GIM" && $(i+2)~/r100595/)
        print $3, $i, $(i+2)
  }
' RS=\< file

Code:
awk '/<api/{s=$NF} /r100595/ && p=="BUNDLE-GIM" {print s, p, $NF} {p=$NF}' file


Last edited by Scrutinizer; 02-27-2018 at 11:48 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 02-27-2018
I like this quite a bit!
Code:
awk '/<api/{s=$NF} /r100595/ && p=="BUNDLE-GIM" {print s, p, $NF} {p=$NF}' file

as it is very clean.

Thank you, Scrutinizer.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

awk getline problem

Hello, I want to print out the DNA sequence entries (tens of thousand!) that are longer than certain value (i=200) from a file (FASTA file) as: >S94D_ctg_8004 Average coverage: 402.95 ATAATGCCTGTGAATATGACATGTGTTCCTGTTTCTACATCAGACTACTATTCTTGCATA... (12 Replies)
Discussion started by: yifangt
12 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

awk getline question

Hi there, I have an ifconfig output and i want to write a script that determines whether there is a line "groupname ipmp" on a particular interface here is my example ifconfig -a output lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1 inet 127.0.0.1... (2 Replies)
Discussion started by: rethink
2 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

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

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

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