Operating System - HP-UX
1819668 Members
2930 Online
109605 Solutions
New Discussion

how to assign mutiple matches to variable

 
SOLVED
Go to solution
lawrenzo
Trusted Contributor

how to assign mutiple matches to variable

I have a report that I need to read and assign certain variable from each lineL

 

-
HOSTNAME:     londewer

VioIP:       16.18.22.134
VioGW:       16.18.22.1
VioMask:     255.255.255.0
VioEn:       en10 (ent10: =ent1, =ent3)
Man:      man 7

CONS 16.18.21.201 is raider01
CONS16.18.21.202 is raider02

 

I want to search the host name and the values of CONS and print in a comma delimitted line ....

 

I am struggling with searching CONS so the above output I want to see

 

londewer:raider01:raider02

 

I am attempting to use awk with no avail

 

Thanks in advance


 

hello
3 REPLIES 3
Hein van den Heuvel
Honored Contributor
Solution

Re: how to assign mutiple matches to variable

 

Hello Lawrenzo,

 

- Are there multiple segments in the input file, or just one HOSTNAME and its attributes

- Is the spacing as erratic/unpredicatble as your example suggest?

 

I suggest you try this awk one-liner

 

$ awk '/HOSTNAME/{if (line != "") print line; line =$NF}  /CONS/{line=line ":" $NF} END {print line}'  < tmp.tmp
londewer:raider01:raider02

 

In slow motion:

 

/HOSTNAME/{    # if the line has HOSTNAME on it

if (line != "") print line;      # if there was a line ready, then print it.

line =$NF}                   #  start a new line, using the last field on the line.

/CONS/{                # if  the line has the word CONS on it

line=line ":" $NF       #  append a colon and the last field to a current line.}

END {print line}        # print the last (first!?) line built-up

 

IF you know for sure the match words have no spaces before them, then you shoudl 'anchor' the expressions like so: /^HOSTNAME/ and /^CONS/

 

If there is just one section, then the "if (line..." is superfluos

 

Enjoy!

Hein

 

btw... if this does NOT help you solve the problem, then please be sure to re-reply and  ATTACH a realistic input example as .TXT file.

 

 

 

 

lawrenzo
Trusted Contributor

Re: how to assign mutiple matches to variable

Thank you Hein,

 

I will take a look at that now - and thanks for the "slow motion" it helps a lot.

 

Chris.

hello
lawrenzo
Trusted Contributor

Re: how to assign mutiple matches to variable

I even managed to hack around with the command and take a bit more input that is important not sure if its the best way but it works for me and gives me exaclty what I was looking for!

 

------------------------------
HOSTNAME:     londew
ViosIP:       x

ViosGW:       x

ViosMask:     255.255.255.0
ViosEn:       en10 (=en1, =en3)
CONS 169.183.18.202 is raider1

CONS 169.183.18.201 is raider2

------------------------------
ok: BMC: Fileset IHS.BMC.perf.agent.is installed
ok: BMC: daemon listening on port 10128
ok: BMC: daemon bgssd in /etc/inetd.conf
ERROR: BMC: user perform does not have valid CONS () in /usr/adm/best1_default/local/setup/Collect.cfg

 

check BMC |awk '/^HOSTNAME/{if (line != "") print line; line =$NF ":"} /^HMC/{line=line $NF ":"} /BMC/ {line=line $1} END {print line}'

 

londew:cons2:cons1:ok:ok:ok:ERROR:

 

thanks again!

hello