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

AWK help 2

Status
Not open for further replies.

santhas

Technical User
Aug 27, 2003
35
AU
I have two files

File1
filer_xxxxx_Wed_Full
/vol/vol1/qtree
....
....

File2
/vol/vol1/qtree

I want read File2 and if the line in File2 matches a line in File1 then print the line previous to matching line.

I tried with this command

for ln in `cat /tmp/File2`; do nawk -v var=$ln '/var/{print $0};{x=$0}' /tmp/File1; done

But it returns nothing, but I know there are matching condition.

Could someone help me here?

Thanks
 
what about this ?
Code:
for ln in `cat /tmp/File2`; do  nawk -v var=$ln '$0~var{print x}{x=$0}' /tmp/File1; done

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks for the input. It didn't help me. It prints everything from File2. I want to print the line above the matching line from File1 ( Ex: filer_xxxxx_Wed_Full)

Thanks
 
PHV's solution should work fine, did you remember to change print $0 to print x?

Another pure awk solution would be:

Code:
awk '
        NR==FNR { targets[$1]=1 ; next }
        $0 in targets { print x }
        { x=$0 }
' File2 File1

This presumes that File2 contains whole lines to match in File1.

Annihilannic.
 
Thanks Guys. Both worked for me. Appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top