Need some help matching a field in one file with a field in another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need some help matching a field in one file with a field in another
# 1  
Old 07-20-2010
Error Need some help matching a field in one file with a field in another

One file (file1) is as such:
Code:
Abc.txt 1.1 1.3
Abc_v2.txt 1.1 1.4
Tree.txt 1.3 1.4
Grass.txt 1.3 4.5


The other (file2):
Code:
Horse.txt 1.1 ref23232
Abc.txt 1.1 1.2 ref 3232-3232 1.3 ref 3232-3232
Plane.txt 1.1 1.2 ref 3232-3232  1.3 ref 3232-3232  1.4
Tree.txt 1.3 ref 3232-3232 1.4
Grass.txt 1.3 ref 3232-3232 



I want to match the first field (word 1) of file 1 with the first field (word 1) of file 2, and if there is a match then I want to put the line from file 2 into a separate file (done with the > at the end I'm guessing). So the output I would like is:

Code:
Abc.txt 1.1 1.2 ref 3232-3232 1.3 ref 3232-3232
Tree.txt 1.3 ref 3232-3232 1.4
Grass.txt 1.3 ref 3232-3232 


I tried solutions suggested by rdcwayx:
Code:
awk 'NR==FNR{a[$1]=1;next} a[$3] {print}' File1 File2


but to no avail. All help would be much appreciated, thanks.
# 2  
Old 07-20-2010
The field positions are not matching. Try:

Code:
awk 'NR==FNR{a[$1]=$1;next} a[$1]' File1 File2

# 3  
Old 07-20-2010
Quote:
Originally Posted by zaxxon
The field positions are not matching. Try:

Code:
awk 'NR==FNR{a[$1]=$1;next} a[$1]' File1 File2

Tried that mate, didn't work Smilie
# 4  
Old 07-20-2010
Quote:
Originally Posted by linuxkid
Tried that mate, didn't work Smilie
Hi.

I just tried it, and it works perfectly:

Code:
$ awk 'NR==FNR{a[$1]=$1;next} a[$1]' file1 file2
Abc.txt 1.1 1.2 ref 3232-3232 1.3 ref 3232-3232
Tree.txt 1.3 ref 3232-3232 1.4
Grass.txt 1.3 ref 3232-3232

Can you say what didn't work? That would be more helpful Smilie
# 5  
Old 07-20-2010
Network

Quote:
Originally Posted by scottn
Hi.

I just tried it, and it works perfectly:

Code:
$ awk 'NR==FNR{a[$1]=$1;next} a[$1]' file1 file2
Abc.txt 1.1 1.2 ref 3232-3232 1.3 ref 3232-3232
Tree.txt 1.3 ref 3232-3232 1.4
Grass.txt 1.3 ref 3232-3232

Can you say what didn't work? That would be more helpful Smilie
Basically just copied it out as you did (whitespace 'n' all), but it comes up with the message:

Code:
awk 'NR==FNR{a[$1]=$1;next} a[$1]' file1 file2

Code:
awk: syntax error near line 1
awk: bailing out near line 1

and I seriously don't understand why!!



WORKED IT OUT! I WAS ON SOLARIS SO I JUST PUT /usr/bin/nawk AT THE FRONT!

Last edited by linuxkid; 07-20-2010 at 12:15 PM..
# 6  
Old 07-20-2010
another way

Code:
#!/bin/bash
cut -d' ' -f1 file1 > file3
grep -f file3 file2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk to add length of matching characters between field in file

The awk below produces the current output, which will add +1 to $3. However, I am trying to add the length of the matching characters between $5 and $6 to $3. I have tried using sub as a variable to store the length but am not able to do so correctly. I added comments to each line and the... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

awk to update field using matching value in file1 and substring in field in file2

In the awk below I am trying to set/update the value of $14 in file2 in bold, using the matching NM_ in $12 or $9 in file2 with the NM_ in $2 of file1. The lengths of $9 and $12 can be variable but what is consistent is the start pattern will always be NM_ and the end pattern is always ;... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

Need to replace last field in a file,if first field matches

Hi, Need to replace last field in a file(/etc/passwd) ,if first filed matches with particular username. Scenario: cat testfor1 deekshi:x:7082:7082::/home/deekshi:/bin/bash harini1:x:7083:7083::/home/harini1:/bin/bash Here,if first field contains "deekshi", then i should replace... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

4. Shell Programming and Scripting

Command/script to match a field and print the next field of each line in a file.

Hello, I have a text file in the below format: Source Destination State Lag Status CQA02W2K12pl:D:\CAQA ... (10 Replies)
Discussion started by: pocodot
10 Replies

5. Shell Programming and Scripting

Matching the header of a .CSV file with dynamic field names

I have a .CSV file (frequency - weekly) whose header contains the year-week value in two of the columns which keeps changing every week. For an instance please see below. Column1,Column2,Column3,Column4,Column5,Column6,Column7,Column8,Column9,Column10,Column11,Column12,Column13,201420... (4 Replies)
Discussion started by: dhruuv369
4 Replies

6. Linux

How do I format a Date field of a .CSV file with multiple commas in a string field?

I have a .CSV file (file.csv) whose data are all enclosed in double quotes. Sample format of the file is as below: column1,column2,column3,column4,column5,column6, column7, Column8, Column9, Column10 "12","B000QRIGJ4","4432","string with quotes, and with a comma, and colon: in... (3 Replies)
Discussion started by: dhruuv369
3 Replies

7. Shell Programming and Scripting

Plz Help. Compare 2 files field by field and get the output in another file.

Hi Freinds, I have 2 files . one is source.txt and second one is target.txt. I want to keep source.txt as baseline and compare target.txt. please find the data in 2 files and Expected output. Source.txt 1|HYD|NAG|TRA|34.5|1234 2|CHE|ESW|DES|36.5|134 3|BAN|MEH|TRA|33.5|234... (5 Replies)
Discussion started by: i150371485
5 Replies

8. Shell Programming and Scripting

Compare two files Field by field and output the result in another file

Hi Friends, Need Help. I have file1.txt as File1.txt |123|A|7267|Hyder|Cross|Sell|7801 |995|A|7051|2008|Lunar|New|Year|Promotion|7801 |996|A|7022|Q108|Targ|Prospect|&|SSCC|Savings|Promo|7801 |997|A|7182|Q1|Feb-Apr|08|Credit|ITA|PA|SBA|Campaign|7801 File2.txt... (7 Replies)
Discussion started by: i150371485
7 Replies

9. Shell Programming and Scripting

Append 1st field from a file into 2nd field of another file

Hi, I've internally searched through forums for about 2+ hours. Unfortunately, with no luck. Although I've found some cases close to mine below, but didn't help so much. Actually, I'm in short with time. So I had to post my case. Hoping that you can help. I have 2 files, FILE1 ... (1 Reply)
Discussion started by: amurib
1 Replies

10. Shell Programming and Scripting

Appending 1st field in a file into 2nd field in another file

Hi, I've internally searched through forums for about 2+ hours. Unfortunately, with no luck. Although I've found some cases close to mine below, but didn't help so much. Actually, I'm in short with time. So I had to post my case. Hoping that you can help. I have 2 files, FILE1 ... (0 Replies)
Discussion started by: amurib
0 Replies
Login or Register to Ask a Question