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

Why does this not work...

Status
Not open for further replies.

sbgirl54

Programmer
Aug 16, 2012
7
FR
This doesn't work:

Code:
awk '
      NR == FNR
      { x=$1 ; next }
      { y=$1 }
      {if (x==y) print $0}
    '  file1.in catalog.cat > file2.res

I'm trying to make a list of catalog entries that match the line headers in file1. The program instead outputs a copy of file1. That doesn't make much sense...
 
What about this ?
Code:
awk 'NR==FNR{x[$1];next}$1 in x' file1.in catalog.cat > file2.res

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
The reason you ended up with a copy of file1.res in file2.res with your original solution is the "NR == FNR" line. Because it isn't followed by a code block on the same line, the default action when that expression is true is to print the current input line. The rest of your code wasn't generating any output.

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top