Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grep text strings between repeating lines 1

Status
Not open for further replies.

viadisky

Technical User
Jun 19, 2003
110
GB
Hi,

I have this data to manipulate. Basically, all I need to do is to put "SYMBOL:" and "Parent Submap:" values side-by-side.

SYMBOL: 172.21.203.8
Object Type: 3
Parent Submap: Alabama
Symbol Type: Network:IP Network
Label: 172.21.203.8
Symbol Position: 1:721x644+140+152
Hidden Value: FALSE
Submap Exists: TRUE
Background Graphics:
Layout Style: 1
Layout On: TRUE
Submap Overlay: TRUE
Show Connection Labels: FALSE
END SYMBOL
SYMBOL: 172.21.203.20
Object Type: 3
Parent Submap: New York
Symbol Type: Network:IP Network
Label: 172.21.203.20
Symbol Position: 1:721x644+92+283
Hidden Value: FALSE
Submap Exists: TRUE
Background Graphics:
Layout Style: 1
Layout On: TRUE
Submap Overlay: TRUE
Show Connection Labels: FALSE
END SYMBOL


This is the output I need:

172.21.203.8 Alabama
172.21.203.20 New York


Please help me :)

Cheers,
Maria :)
 
A starting point:
awk '/SYMBOL:/{s=$NF}/Parent Submap:/{print s,$NF}' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi

I think there is no way to do it with [tt]grep[/tt].
Code:
sed -n '/SYMBOL/{s/.*: //;h};/Parent Submap/{s/.*: //;G;s/\(.*\)\n\(.*\)/\2 \1/;p}' /input/file
[gray]# or[/gray]
awk -F: '/SYMBOL/{s=$2}/Parent Submap/{print s,$2}' /input/file

Feherke.
 

Thanks for your suggestion PHV but because awk specifically greps the last field, New York became York.

I tried Feherke's awk one-liner :

awk -F: '/SYMBOL/{s=$2}/Parent Submap/{print s,$2}' /input/file

It did work! I am just a little worried on the types of data I have ... I might have a Parent Submap which looks like this:

Parent Submap: New York:eek:ffice1

I think only office1 will be picked ...

Thank you for your quick replies.

Cheers,
Maria
 
Hi

No, in that case only "New York" will be outputed, "office1" would by the third field, called $3. If you want all the rest of the line, could be easier just replacing the first field and keep the rest :
Code:
awk -F: '/SYMBOL/{s=$2}/Parent Submap/{$1=s;print}' /input/file

Feherke.
 
Hi Feherke,

You're right ...

I tried the one-liner awk you gave, it worked okay.


Many,many THANKS,
Maria :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top