1833883 Members
1484 Online
110063 Solutions
New Discussion

Re: awk format help

 
SOLVED
Go to solution
Denver Osborn
Honored Contributor

awk format help

I want to take the output of cmgetconf and collect the HEARTBEAT_IP info per node. I'd like the output similar to

node1_name:lan0:1.1.1.1
node1_name:lan1:2.1.1.1
node2_name:lan0:1.1.1.2
node2_name:lan1:2.1.1.2

So far when it comes to awk all I'm good for is pattern matching. :) Sorry, I don't need it in perl for all the perl gurus out here.

This should be some easy points for someone.

Thanks!
-denver
9 REPLIES 9
Steven E. Protter
Exalted Contributor

Re: awk format help

Shalom,

Here is how I'd extact.

Data is in a file say

while read -r data
do
nodename=$(echo $data | awk -F: '{print $1}'
lannumber=$(echo $data | awk -F: '{print $2}'
ipaddy=$(echo $data | awk -F: '{print $3}'
done < file

Not the most efficient code but brutally effective.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Denver Osborn
Honored Contributor

Re: awk format help

Sorry, I should've provided more detail.

I'm taking the output of cmgetconf and want to pull the node name, lan and ip for each heartbeat ip defined.

awk '/\ HEARTBEAT_IP|\ NETWORK_INTERFACE|NODE_NAME/&&!/^#/{print $NF}' /tmp/cmgetconf.out


Above awk gets me this...

node1_name
lan0
1.1.1.1
lan1
2.1.1.1
node2_name
lan0
1.1.1.2
lan1
2.1.1.2

But what I really want is the output format to be...

node1_name:lan0:1.1.1.1
node1_name:lan1:2.1.1.1
node2_name:lan0:1.1.1.2
node2_name:lan1:2.1.1.2


I'm not sure what need to be done w/ awk to get it there.

-denver
spex
Honored Contributor

Re: awk format help

Hi Denver,

This little awk script is based on cmgetconf output I found on the web, so I'm not sure that it will work for your data...

/^NODE_NAME/ {
nn=$2
}
/^NETWORK_INTERFACE/ {
ni=$2
}
/^STATIONARY_IP/ {
si=$2
comp=1
}
comp {
printf("%s:%s:%s\n",nn,ni,si)
comp=0
}

PCS
Hein van den Heuvel
Honored Contributor

Re: awk format help

I don't have cmgetconf output here to test with, but judging buy your output, you just need to save teh last fields on the lines in variables and then print all on the last line. Something like


awk '/ HEARTBEAT_IP/ print name ":" int ":" $NF} /NETWORK_INTERFACE/{int=$NF} /{name=$NF} |NODE_NAME/&&!/^#/{print $NF}' /tmp/cmgetconf.out

hth,
Hein.
Hein van den Heuvel
Honored Contributor

Re: awk format help

I don't have cmgetconf output here to test with, but judging buy your output, you just need to save teh last fields on the lines in variables and then print all on the last line. Something like:

awk '/^HEARTBEAT_IP/ print name ":" int ":" $NF} /^NETWORK_INTERFACE/{int=$NF} /^NODE_NAME/{name=$NF}' /tmp/cmgetconf.out

hth,
Hein.
Hein van den Heuvel
Honored Contributor

Re: awk format help

Ooops, sorry for the double, and now triple post. Slip of the mouse!
To make up, some matching hints:

By testing for /^word/ we force the begin-of-line anchor, making the matching process quicker, and making commented lines not match.
You may want to use /^ *word/ to allow for zero or more spaces before the word, but still at line start.

An other way to deal with comments is to have the FIRST check be:
/ *#/ { next }

Cheers,
Hein.

Denver Osborn
Honored Contributor

Re: awk format help

thanks everyone, this should help a lot. Attached is cmgetconf output, cleaned up just a bit.

spex
Honored Contributor
Solution

Re: awk format help

/^NODE_NAME/ {
nn=$2
}
/^ *NETWORK_INTERFACE/ {
ni=$2
}
/^ *HEARTBEAT_IP/ {
ip=$2
comp=1
}
comp {
printf("%s:%s:%s\n",nn,ni,ip)
comp=0
}
Denver Osborn
Honored Contributor

Re: awk format help

Thanks Hein for your example and explanations.

Also to spex for running his example against my cmgetconf output, it's what I was looking for.

-denver