Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-13-2012
Registered User
 
Join Date: Apr 2012
Posts: 17
Thanks: 0
Thanked 1 Time in 1 Post
AWK - Different outputs *Question*

I'm having a small issue with AWK:

I run this in PUTTY:

Code:
awk 'BEGIN{FS=","}NR==FNR{A[$1]=$1;next}{if (A[$1]==$1) print $0}' FILE1 FILE2

And it gives me the output that I expect.

I wanted to create a file.awk file that i could just run instead of typing this out all the time. But its not giving my expected output. Here is what I did:


Code:
 #VLOOKUP type funtion useing AWK
BEGIN{FS=","}
NR==FNR
{A[$1]=$1;next}
{if (A[$1]==$1) print $0}

Then I run this:


Code:
 awk -f ultVlookup.awk FILE1 FILE2

But I'm not getting the same results.
What am I doing wrong?
Sponsored Links
    #2  
Old 04-13-2012
Mead Rotor
 
Join Date: Aug 2005
Location: Saskatchewan
Posts: 16,374
Thanks: 491
Thanked 2,535 Times in 2,418 Posts
The NR==FNR controls whether the code block behind it runs.

When you put the code block on a different line, the statement is used for a different purpose -- determines whether the line prints. And the code block, without anything in front of it, runs every line, and the last line is never run because of the 'next' in the line above.


Code:
BEGIN{ FS="," }
NR==FNR {A[$1]=$1;next}
{if (A[$1]==$1) print $0}

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
create outputs from other command outputs rajsharma Shell Programming and Scripting 1 07-14-2010 07:43 AM
strcat outputs garbage airon23bball Programming 2 05-25-2010 01:57 PM
compare 2 outputs melanie_pfefer Shell Programming and Scripting 1 05-21-2008 09:20 AM
Printing outputs using awk. Krrishv Shell Programming and Scripting 4 01-09-2007 06:31 AM
I may have accidentally redefined one of the outputs... joang Solaris 2 03-22-2005 09:12 AM



All times are GMT -4. The time now is 10:03 AM.