Comparing two files and printing 2nd column if match found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing two files and printing 2nd column if match found
# 1  
Old 11-09-2010
Comparing two files and printing 2nd column if match found

Hi guys,

I'm rather new at using UNIX based systems, and when it comes to scripting etc I'm even newer.

I have two files which i need to compare.

file1: (some random ID's)

451245
451288
136588
784522


file2: (random ID's + e-mail assigned to ID)

123888 xc@xc.com
451245 a@a.com
122112 adsadas@asd.com
451288 b@b.com
136588 c@c.com
784522 d@d.com

My thoughts on this, was to have the script print $2 of file2 if there's a match on that particular ID from in the File1.

So it cant do a per line search i guess, it needs to look throughout the file and search for a match.
I would like it to store the output in a .txt file aswell - I know you can do that by > randomfile.txt right?

I hope you get what I'm talking about :-D


Best regards
Jesper
# 2  
Old 11-09-2010
try
Code:
awk 'NR==FNR{a[$1,$2]=$3;next} a[$1,$2]{print $2}' file2 file1

# 3  
Old 11-09-2010
Hi, thanks for answering. I'm getting this when trying to run it.

awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1
# 4  
Old 11-09-2010
do you have solaris? if so; try:

Code:
nawk 'NR==FNR{a[$1,$2]=$3;next} a[$1,$2]{print $2}' file2 file1

# 5  
Old 11-09-2010
Yeah im using solaris, and nawk did the trick. Not getting any errors now :-) I doesn't seem to print anything out tho, is this wrong?

nawk 'NR==FNR{a[$1,$2]=$3;next} a[$1,$2]{print $2}' file2 file1 > output.txt
# 6  
Old 11-09-2010
you should see the result. something is wrong. try:

Code:
nawk 'NR==FNR {a[$1$2]++;next} a[$1$2] { print $2 }' file2 file1



you can find similar detailed sol'n at:
https://www.unix.com/shell-programming-scripting/147237-easily-search-file-get-line-little-problem-post302466738.html
# 7  
Old 11-09-2010
Use grep with the -f option. Check your man page.

Quote:
Originally Posted by gc_sw
you should see the result. something is wrong. try:

Code:
nawk 'NR==FNR {a[$1$2]++;next} a[$1$2] { print $2 }' file2 file1



you can find similar detailed sol'n at:
https://www.unix.com/shell-programming-scripting/147237-easily-search-file-get-line-little-problem-post302466738.html
That shouldn't work since the first file has only one column.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing two columns in two files and printing a third based on a match

Hello all, First post here. I did not notice a previous post to help me down the right path. I am looking to compare a column in a CSV file against another file (which is not a column match one for one) but more or less when a match is made, I would like to append a third column that contains a... (17 Replies)
Discussion started by: dis0wned
17 Replies

2. Shell Programming and Scripting

Comparing two one-line files and selecting what does not match

I have two files. One is consisting of one line, with data separated by spaces and each number appearing only once. The other is consisting of one column and multiple lines which can have some numbers appearing more than once. It looks something like this: file 1: 20 700 15 30 file2: 10... (10 Replies)
Discussion started by: maya3
10 Replies

3. Shell Programming and Scripting

Comparing same column from two files, printing whole row with matching values

First I'd like to apologize if I opened a thread which is already open somewhere. I did a bit of searching but could quite find what I was looking for, so I will try to explaing what I need. I'm writing a script on our server, got to a point where I have two files with results. Example: File1... (6 Replies)
Discussion started by: mitabrev83
6 Replies

4. UNIX for Dummies Questions & Answers

Comparing two test files and printing out the values that do not match

Hi, I have two text files with matching first columns. Some of the values in the second column do not match. I want to write a script to print out the rows (only the first column) where the values in the second column do not match. Example: Input 1 A 1 B 2 C 3 D 4 Input 2 A 2 B 2... (6 Replies)
Discussion started by: evelibertine
6 Replies

5. Shell Programming and Scripting

Printing the line number of first column found

Hello, I have a question on how to find the line number of the first column that contains specific data. I know how to print all the line numbers of those columns, but haven't been able to figure out how to print only the first one that is found. For example, if my data has four columns: 115... (3 Replies)
Discussion started by: user553
3 Replies

6. UNIX for Dummies Questions & Answers

Comparing two text files by a column and printing values that do not match

I have two text files where the first three columns are exactly the same. I want to compare the fourth column of the text files and if the values are different, print that row into a new output file. How do I go about doing that? File 1: 100 rs3794811 0.01 0.3434 100 rs8066551 0.01... (8 Replies)
Discussion started by: evelibertine
8 Replies

7. Shell Programming and Scripting

comparing column of two different files and print the column from in order of 2nd file

Hi friends, My file is like: Second file is : I need to print the rows present in file one, but in order present in second file....I used while read gh;do awk ' $1=="' $gh'" {print >> FILENAME"output"} ' cat listoffirstfile done < secondfile but the output I am... (14 Replies)
Discussion started by: CAch
14 Replies

8. UNIX for Dummies Questions & Answers

Comparing the 2nd column in two different files and printing corresponding 9th columns in new file

Dear Gurus, I am very new to UNIX. I appreciate your help to manage my files. I have 16 files with equal number of columns in it. Each file has 9 columns separated by space. I need to compare the values in the second column of first file and obtain the corresponding value in the 9th column... (12 Replies)
Discussion started by: Unilearn
12 Replies

9. Shell Programming and Scripting

printing words based on column match

pls help Input: file1 word1 text1 word2 text2 word3 text3 file2 word1 text11 word3 text13 can u pls help in getting the same output: file1 text1 text2 text3 (1 Reply)
Discussion started by: bha148
1 Replies

10. Shell Programming and Scripting

reading two files, comparing, printing when unmatched value is seen

Hello, I have two files: file1 1 2 3 4 5 file2 "a","b",,,,,"c","1",..... "s","d",,,,,"s","1",..... "a","c",,,,,"d","1",.... "f","v",,,,,,"f","2",..... etc I have to read "1" from file1 and grab all records in file2 (say column 6 in file2 is "1") until the column changes its value (... (6 Replies)
Discussion started by: i.scientist
6 Replies
Login or Register to Ask a Question