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
# 8  
Old 11-09-2010
Hi, I think it might be too advanced for me to understand? I'm not getting anything out of the man page for grep using -f Smilie
# 9  
Old 11-09-2010
If your grep version supports the -f option:
Code:
grep -f file1 file2

Otherwise with awk:
Code:
nawk 'NR==FNR{a[$0]; next}$1 in a' file1 file2

This User Gave Thanks to Franklin52 For This Post:
# 10  
Old 11-09-2010
Using
nawk 'NR==FNR{a[$0]; next}$1 in a' file1 file2 > test.txt

Dosnt seem to do the desired thing..

file1 is a little group of ID's i have which i need to get hold of their email associated to their ID. (926 lines/entries/users)
file2 is the complete list holding all ID's with their email address. (1045 lines/entries/users)

When i use the command, it creates a file containing 842 (lines/entries/users) where ID AND Email is listed.

I only need to get their Email address from their ID if theres a match and create a output file that only contains the emails that were a match.

Best regards
# 11  
Old 11-09-2010
With the given sample files I get:
Code:
$ cat file1
451245
451288
136588
784522
$ cat file2
123888 xc@xc.com
451245 a@a.com
122112 adsadas@asd.com
451288 b@b.com
136588 c@c.com
784522 d@d.com
$ awk 'NR==FNR{a[$0]; next}$1 in a' file1 file2
451245 a@a.com
451288 b@b.com
136588 c@c.com
784522 d@d.com
$

Am I missing something?
This User Gave Thanks to Franklin52 For This Post:
# 12  
Old 11-09-2010
IMO franklin's suggestion just needs a print $2 action. Plus I would tend to use $1 instead of $0 in the first part to avoid possible mismatches due to spurious spacing...
Code:
awk 'NR==FNR{a[$1]; next}$1 in a{print $2}' file1 file2

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 13  
Old 11-10-2010
Thanks guys, I've got it working now :-)
# 14  
Old 11-11-2010
Lets say i now need to search for missing entries between two files. I would like to print $1 from file1 if the number isnt found in file2?

file1(total)
1
2
3
4
5
6

file2 (partial)
1
2
3


output(numbers missing)
4
5
6


Thanks
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