INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

reverse lookup using awk - need help

reverse lookup using awk - need help

(OP)
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

RE: reverse lookup using awk - need help

(OP)
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
=========================

RE: reverse lookup using awk - need help

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
  • when an "edit policy" line is found, you store it for later use
  • when other line than "edit policy" is found, you print the matching previously stored "edit policy" line
Could you correct/confirm the above ?

Feherke.
http://feherke.github.com/

RE: reverse lookup using awk - need help

(OP)
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"

RE: reverse lookup using awk - need help

Try this

CODE

# Run: awk -f awkfw.awk awkfw.txt
$0 ~ /edit[ ]+policy/ {
  edit_policy = $1 " " $2 " " $3
}
$3 ~ /abc/ {
  #print "found: " $1 " " $2 " " $3 "\t--> " edit_policy
  print edit_policy   
} 

RE: reverse lookup using awk - need help

(OP)
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.

RE: reverse lookup using awk - need help

(OP)
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.

RE: reverse lookup using awk - need help

(OP)
Discard my previous message. I am all set. Thanks again :)medal

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close