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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reverse lookup using awk - need help 1

Status
Not open for further replies.

awkfw

Technical User
Dec 24, 2012
6
US
TEST FILE:
===========
edit policy mars
set src abc
edit policy abc
edit policy yahoo
edit policy bbc
edit policy cnn
edit policy XXX
set dst abc
edit policy yyy
set src abc
=========================



What I want to do here is find "abc" in the text file (could be 100+ lines) and only display lines if the line starts with "edit policy" with word 'abc' in it e.g. "edit policy abc" and if it finds word "abc" in any other line not starting with "edit policy" e.g. "set src" or "set dst" then perform a reverse lookup from that matching line and print the first line starting with "edit policy".


e.g. the desired output of the search would be like following:
edit policy mars
edit policy abc
edit policy XXX
edit policy yyy
 
By the way, the reason why I am not able to use "grep -B" the file may contain # of "set" commands with other variables before "edit policy" command e.g. the file could look this this :

TEST FILE:
===========
edit policy mars
set src abc
edit policy abc
edit policy yahoo
edit policy bbc
set dst sds
set src ggg
edit policy cnn
edit policy XXX
set src mbm
set dst btg
set dst abc
edit policy yyy
set dst lkk
set src jhl
set dst bbb
set dst lki
set src hkj
set src abc
=========================
 
What have you tried so far and where in your code are you stuck ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi

I am missing something about the required output. It contains for example "edit policy XXX", but as I understand, that should not appear as there is no "set src XXX" nor "set dst XXX" in the input. From your words I understood that
[ul]
[li]when an "edit policy" line is found, you store it for later use[/li]
[li]when other line than "edit policy" is found, you print the matching previously stored "edit policy" line[/li]
[/ul]
Could you correct/confirm the above ?

Feherke.
[link feherke.github.com/][/url]
 
Thanks for your response. Let me explain the output requirement one more time. My apologies if it was not clear. I am not a programmer.

TEST FILE:
===========
edit policy mars
set src abc
edit policy abc
edit policy yahoo
edit policy bbc
set dst sds
set src ggg
edit policy cnn
edit policy XXX
set src mbm
set dst btg
set dst abc
edit policy yyy
set dst lkk
set src jhl
set dst bbb
set dst lki
set src hkj
set src abc
=========================



I want to search for keyword "abc" in above file. The output requirement is:
- If it matches the line starting with "edit policy" then print that line.
- If it matches the line which does not start with "edit policy" e.g. "set src abc" then start a reverse lookup and display the first line with "edit policy .... " match.

e.g. the desired output of the search would be like following:
edit policy mars
edit policy abc
edit policy XXX
edit policy yyy




Output explanation:


edit policy mars ---> Display this line because it matched the "set src abc" but could not find the "edit policy" words in the matching line so it performed the reverse lookup and displayed the first matching line e.g."edit policy mars"

edit policy abc --> this is straight forward match because it matches the line with abc which happen to also start with "edit policy".


edit policy XXX --> Display this line because it matched the "set dst abc" but could not find the "edit policy" words in the matching line so it performed the reverse lookup and displayed the first matching line e.g."edit policy XXX"

edit policy yyy --> Display this line because it matched the "set src abc" but could not find the "edit policy" words in the matching line so it performed the reverse lookup and displayed the first matching line e.g."edit policy yyy"

 
Try this
Code:
[COLOR=#0000ff]# Run: awk -f awkfw.awk awkfw.txt[/color]
[COLOR=#6a5acd]$0[/color] ~ [COLOR=#ff00ff]/edit[[/color][COLOR=#6a5acd] [/color][COLOR=#ff00ff]][/color][COLOR=#6a5acd]+[/color][COLOR=#ff00ff]policy/[/color] {
  edit_policy = [COLOR=#6a5acd]$1[/color] [COLOR=#ff00ff]" "[/color] [COLOR=#6a5acd]$2[/color] [COLOR=#ff00ff]" "[/color] [COLOR=#6a5acd]$3[/color]
}
[COLOR=#6a5acd]$3[/color] ~ [COLOR=#ff00ff]/abc/[/color] {
  [COLOR=#0000ff]#print "found: " $1 " " $2 " " $3 "\t--> " edit_policy[/color]
  [COLOR=#804040][b]print[/b][/color] edit_policy   
}
 
Thank you mikrom, your suggested code worked the way I wanted. Amazed by a simple solution like this. I was over thinking of achieving this using "for" loop with NR-- nonsense. But it appears sometimes there is any easy solution if you think creatively. Thanks again and Happy new year.
 
Not a big deal but could we do the whole code in one liner? Again, it is not a big deal but it is more just my curiosity.
 
Discard my previous message. I am all set. Thanks again :)[medal]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top