NR==FNR for getting Col4 value from 2nd file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting NR==FNR for getting Col4 value from 2nd file
# 1  
Old 06-30-2010
NR==FNR for getting Col4 value from 2nd file

Multiple versions of this are probably in this section, but could not find one just right for me. My code gives me a zero byte file.

Problem:
Code:
foo
553
403
448
492
451
403
456
560
527
534
529
550
500
447
404
394

Code:
bar
1    396    honda    4
2    406    mercedes    6
3    418    bmw    5
4    423    ford    4
5    429    toyota    4
6    445    nissan    4
7    451    volkswagen    4
8    457    audi    4
9    459    chevrolet    2
10    470    renault    4
11    472    mazda    2
12    478    peugeot    3
13    508    mitsubishi    4
14    511    dodge    4
15    522    volvo    4
16    525    fiat    4

Code:
awk 'NR==FNR{A[$2]=$3;next} $2=A[$1]' OFS="\t" foo bar >barfoo

What am I doing wrong?
Want to have the car names lined up against the 3digit numbers in col1 of first file.
# 2  
Old 06-30-2010
Code:
awk 'NR==FNR{a[$2]=$3;next}{print $0,a[$0]}' OFS="\t" bar foo > barfoo

# 3  
Old 06-30-2010
Can you point out what is wrong with my awk code/
Yours works by the way, thank you..
though it does not work the way I expected for my actual data where I need the following:

Code:
foo
rs6701312
rs6698901
rs561289
rs12128446
rs10128022

Code:
bar
1       rs642248        0       78376434        1       3
1       rs12126871      0       78378487        3       1
1       rs561289        0       78383457        1       4
1       rs11162418      0       78395479        3       2
1       rs689374        0       78411832        2       4

output
Code:
rs561289        78383457

I think I switched the order of foo and bar.
bar should be first and foo should be second.. right?

Last edited by genehunter; 06-30-2010 at 07:52 PM.. Reason: Got it. switched foo and bar order
# 4  
Old 06-30-2010
Code:
awk 'NR==FNR{a[$2]=$3;next}{if ($0 in a) print $0,a[$0]}' OFS="\t" bar foo > barfoo

As for your code... I think you should read some AWK tutorial and make yourself familiar with the basics Smilie
# 5  
Old 06-30-2010
Code:
awk 'NR==FNR{a[$0];next} $2 in a {print $2,$4}' OFS='\t' foo bar > barfoo

This User Gave Thanks to vgersh99 For This Post:
# 6  
Old 06-30-2010
Quote:
Originally Posted by genehunter
I think I switched the order of foo and bar.
bar should be first and foo should be second.. right?
Indeed, that was the only issue:
Code:
$ awk 'NR==FNR{A[$2]=$3;next} $2=A[$1]' OFS="\t" bar foo
451     volkswagen

This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 07-06-2010
Thank you all. That solves it!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using NR==FNR Command

Dear All, I have below two files with me: file 1: A|B E|F C|D file 2: A|X|Y R|T|I C|V|N I want to compare 1st column of each file and than print both columns of file 1 and column 2 and 3 of file 2 Sample required output in regards to above files is below: A|B|X|Y C|D|V|N (5 Replies)
Discussion started by: Nebula
5 Replies

2. Shell Programming and Scripting

Help with Alternative for NR==FNR

Hi, I have an issue with the below script nawk 'NR==FNR{a=$4" "$5}NR>FNR{print NF?$0:a"\n";if(/^cn:/) x=$0}' FS="" in1.txt in2.txt > out1.txt It is taking too long to get a string from in1.text, search for the string in in2.txt and create a new file out1.txt. Is there any alternative way we... (1 Reply)
Discussion started by: Samingla
1 Replies

3. Shell Programming and Scripting

Tip: alternative for NR==FNR in awk

Example: $ cat file1 2 3$ cat file2 1 2 3 4 5 6The following awk script works like a charm, NR==FNR is true for file1, the remainder runs for file2: awk ' NR==FNR {A; next} ($1 in A) ' file1 file2 2 3Now have an empty file1: >file1and run the awk script again. The result is empty... (8 Replies)
Discussion started by: MadeInGermany
8 Replies

4. Shell Programming and Scripting

How to use NA and FNR?

Hi i have file1: conn=232257 client=16218.19488.218.86:51237 protocol=LDAP file2: conn=232257 dn="uid=apple,ou=xxxx,ou=usfgfhfers,dc=example,dc=com" conn=232370 dn="uid=ball,ou=yyyyyy,ou=usfhfhfhers,dc=example,dc=com" In the output file it should match first column from above both files... (2 Replies)
Discussion started by: buzzme
2 Replies

5. Shell Programming and Scripting

NR==FNR confusions

This has been asked and answered hundreds of times, but I can't understand the syntax of awk's NR==FNR trick for merging files and printing the correct columns. Here's my File 1 1 rs8179466 224176 A ADD 1037 1.066 0.1421 0.8065 1.408 0.4468 ... (3 Replies)
Discussion started by: roofus
3 Replies

6. Shell Programming and Scripting

Awk FNR==NR question

awk -F'' 'FNR==NR {a=$2; next} {$1=a} 1' $useralias ${entries} >> ${entries}_2 Hi, Is there anyway to alter this command so that if it does not find a match it will just leave the line alone instead of replacing what it doesn't find with a blank space? (4 Replies)
Discussion started by: Jazmania
4 Replies

7. Shell Programming and Scripting

NR=FNR

cat file1 1 a b c 2 d e f 3 a f r cat file2 a c e output should be 1 3 means: if field 1 of file2 matches filed 2 of file1 then print field 1 of file1 I know that it can be done using awk NR=FNR. But not able to acheive it. Thanks in advance. (9 Replies)
Discussion started by: shaan4uster
9 Replies

8. Shell Programming and Scripting

FNR>2 is not working and unable to extract data from a file

Hi, I have a file in windows environment and moved to UNIX through FTP (ASCII format). The file is having with tab delimited file. awk ‘FNR>2' file_cust*.txt >>filnal.txt I have the same file in production; it is working fine with the same procedure. Once we receive the file in windows... (1 Reply)
Discussion started by: onesuri
1 Replies

9. Shell Programming and Scripting

remove values of a file one by one from 2nd file and then print the remaining values of 2nd file

Hi all, I have 2 files. One contains only 1 column and other one contains 2 columns, let say 1_col.txt and 2_col.txt respectively. Here, I will try to explain with an example. Input files : 1_col.txt 2_col.txt a a b x a c p ... (5 Replies)
Discussion started by: AshwaniSharma09
5 Replies

10. Shell Programming and Scripting

Awk: different between NR and FNR

As I know: FNR: The ordinal number of the current record in the current file. NR: The ordinal number of the current record from the start of input. I don't understand really differency between NR and FNR. Who can explain it for me? And give me an example. Thanks (1 Reply)
Discussion started by: anhtt
1 Replies
Login or Register to Ask a Question