AWK - Different outputs *Question*


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers AWK - Different outputs *Question*
# 1  
Old 04-13-2012
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?
# 2  
Old 04-13-2012
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}

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing awk outputs

Hello All, I have the following command which works partially: gzcat *2016-03-25_*gz | gawk -F"|" ' BEGIN{format = "%-10s %-13s %-17s %-35s\n"; printf format, "EVENT_TYPE","RESPONSE_CODE","INTERNAL_ERR_CODE","FLOWNAME"; printf format, "----------", "-------------", "-----------------",... (6 Replies)
Discussion started by: EAGL€
6 Replies

2. UNIX for Dummies Questions & Answers

Red Hat Linux question on top and vmstat outputs

RHEL 5.4 Our Linux machine seemed to be running slow. So, I ran the top and vmstat commands. Question1. I can see the process 11517 consuming 100% CPU . But that just means that this process totally utilizes one of the cores in a mult-core CPU. Right ? This machine apparently has two... (2 Replies)
Discussion started by: kraljic
2 Replies

3. Shell Programming and Scripting

awk question : system output to awk variable.

Hi Experts, I am trying to get system output to capture inside awk , but not working: Please advise if this is possible : I am trying something like this but not working, the output is coming wrong: echo "" | awk '{d=system ("date") ; print "Current date is:" , d }' Thanks, (5 Replies)
Discussion started by: rveri
5 Replies

4. Shell Programming and Scripting

awk giving different outputs each time

I have a strange issue. (awk '$3 == "nfs" { cnt++ }; END { print cnt }' /etc/fstab) This is giving different count each time. To test this, tried the one here -bash-3.2$ awk '/nfs/{print $2}' /etc/fstab | wc -l 151 -bash-3.2$ awk '/nfs/{print $2}' /etc/fstab | wc -l 145... (6 Replies)
Discussion started by: sureshmsi
6 Replies

5. Shell Programming and Scripting

Array in awk outputs multiple values

Disclaimer: OP is 100% Awk beginner. I use this code on ASCII files I need to report against. awk 'BEGIN { tokens = 0 tokens = 0 tokens = 0 } { for (token in tokens) { if ($1 == token){print $0; tokens++;}}} END {for (token in tokens){ if( tokens ==... (1 Reply)
Discussion started by: alan
1 Replies

6. Shell Programming and Scripting

create outputs from other command outputs

hi friends, The code: i=1 while do filename=`/usr/bin/ls -l| awk '{ print $9}'` echo $filename>>summary.csv #Gives the name of the file stored at column 9 count=`wc -l $filename | awk '{print $1}'` echo $count>>summary.csv #Gives just the count of lines of file "filename" i=`expr... (1 Reply)
Discussion started by: rajsharma
1 Replies

7. Shell Programming and Scripting

How to store multiple outputs from an awk command?

x=`echo $line | awk -F "|" '{print $1;print NR}'` How will I get the 2 return values ($1 and NR) from awk to variables? (4 Replies)
Discussion started by: tene
4 Replies

8. UNIX for Dummies Questions & Answers

Basic awk question...getting awk to act on $1 of the command itself

I have a script problem that I am not able to solve due my very limited understanding of unix/awk. This is the contents of test.sh awk '{print $1}' From the prompt if I enter: ./test.sh Hello World I would expect to see "Hello" but all I get is a blank line. Only then if I enter "Hello... (2 Replies)
Discussion started by: JasonHamm
2 Replies

9. Shell Programming and Scripting

compare 2 outputs

Hello, I have a shell script that is used as follows: ./script -s <Oracle_server_name> the script returns several lines in this format: parameter name required value Current Value comments --------------- ------------- -------------- --------- My objective now is to compare 2... (1 Reply)
Discussion started by: melanie_pfefer
1 Replies

10. Shell Programming and Scripting

Printing outputs using awk.

I have a output of a command like this. the command is : bdf|sed '/^e/d'|awk '{print$2/1048576}' output : 0 0.515625 0.481979 2 2 2 7.8125 4 2 0.488281 7.8125 3.90625 4 1.95312 1 0.488281 (4 Replies)
Discussion started by: Krrishv
4 Replies
Login or Register to Ask a Question