Compare two sample files and find common


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare two sample files and find common
# 1  
Old 11-05-2012
Compare two sample files and find common

Hi

I have two sample files attached here

one file contain entries in one column and second file contains entries in many columns

I have to match entries of first file with entries in secon d file form secon column onwards and if matches write "match" in front of it.

I tried several programms but not working on my real files thats why attaching here!

Please check it.
# 2  
Old 11-05-2012
Code:
awk 'NR==FNR{X[$0]=$0;next}{for(i in X){if($0 ~ i){gsub(i,i" matched",$0)}}}1' file1 file2


Last edited by pamu; 11-05-2012 at 08:18 AM..
This User Gave Thanks to pamu For This Post:
# 3  
Old 11-05-2012
Hi Pamu,

Thanks for reply. But out put is just like second file there is no change any where! Smilie

Mani
# 4  
Old 11-05-2012
Quote:
Originally Posted by manigrover
But out put is just like second file there is no change any where! Smilie
Please look..

Is this what you want..?

Code:
$ cat file1
pad
cat
dog dd

$ cat file2
afs sdfsdf sdfsdfsdf sfsd pat
ads adas asdasd pat sdfsdfs dog
ads cat cat sdfsdf dog ass pat
adfsadfsdfsdfsdf sdfsdfsd sdfsd
cat cat dog dd pat

$ awk 'NR==FNR{X[$0]=$0;next}{for(i in X){c++;if($0 ~ i){gsub(i,i" matched",$0)}}}1' file1 file2
afs sdfsdf sdfsdfsdf sfsd pat
ads adas asdasd pat sdfsdfs dog
ads cat matched cat matched sdfsdf dog ass pat
adfsadfsdfsdfsdf sdfsdfsd sdfsd
cat matched cat matched dog dd matched pat

Please let me know if you need something else..Smilie
# 5  
Old 11-05-2012
As example my files are like this

Code:
$ cat file1
pad
cat
dog

in different columns second file like this

Code:
$ cat file2
1 afs sdfsdf sdfsdf sfsd 
2 ads adas  asda    pat 
3 ads  cat   cat      sdfsdfs 
4 adfs adfs  dfs      sdfsd
5 cat  cat    dog       pat

column of first file has to match with second column onwards of second file.

Code:
1 afs sdfsdf sdfsdf sfsd 
2 ads adas  asda    pat 
3 ads  cat(matched)   cat(matched)      sdfsdfs 
4 adfs adfs  dfs      sdfsd
5 cat (matched)  cat(matched)    dog(matched)      pat

But many times code doesnt wrk on my real files(as attached) but it works on sample examples...Smiliedont knw why?
# 6  
Old 11-05-2012
Quote:
Originally Posted by manigrover
But many times code doesnt wrk on my real files(as attached) but it works on sample examples...Smiliedont knw why?
Don't know what could be the reason behind this.

Try this to exclude $1 from the search and replace pattern.

As you have space in first file. So we can't use column by column replacement in second file.Smilie

Code:
awk 'NR==FNR{X[$0]=$0;next}{s=$1;$1="";for(i in X){if($0 ~ i){gsub(i,i" (matched)",$0)}};$0=s""$0}1' file1 file2

I hope this helps

pamuSmilie

Last edited by pamu; 11-05-2012 at 08:18 AM..
# 7  
Old 11-05-2012
Hi Pamu,

Thanks a lot. Can we convert file separated by spaces into column separated file first(second file)

Spacing seems to be the only problem in second file as I tried several options because of spacing nothing is working.

let me know if you cme across any suggestion because problem is same still.

Mani
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare two files and print based on common variable value.

Hi All, i have below two files. FILE: NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root" GROUP="disk" MODE="brw-rw----" PKNAME="" MOUNTPOINT="" NAME="/dev/sda1" TYPE="part" SIZE="500M" OWNER="root" GROUP="disk" MODE="brw-rw----" PKNAME="/dev/sda" MOUNTPOINT="/boot" NAME="/dev/sda2"... (3 Replies)
Discussion started by: balu1234
3 Replies

2. Shell Programming and Scripting

Find common files between two directories

I have two directories Dir 1 /home/sid/release1 Dir 2 /home/sid/release2 I want to find the common files between the two directories Dir 1 files /home/sid/release1>ls -lrt total 16 -rw-r--r-- 1 sid cool 0 Jun 19 12:53 File123 -rw-r--r-- 1 sid cool 0 Jun 19 12:53... (5 Replies)
Discussion started by: sidnow
5 Replies

3. Shell Programming and Scripting

Compare multiple files, identify common records and combine unique values into one file

Good morning all, I have a problem that is one step beyond a standard awk compare. I would like to compare three files which have several thousand records against a fourth file. All of them have a value in each row that is identical, and one value in each of those rows which may be duplicated... (1 Reply)
Discussion started by: nashton
1 Replies

4. Shell Programming and Scripting

Compare multiple files, and extract items that are common to ALL files only

I have this code awk 'NR==FNR{a=$1;next} a' file1 file2 which does what I need it to do, but for only two files. I want to make it so that I can have multiple files (for example 30) and the code will return only the items that are in every single one of those files and ignore the ones... (7 Replies)
Discussion started by: castrojc
7 Replies

5. Shell Programming and Scripting

Compare a common field in two files and append a column from File 1 in File2

Hi Friends, I am new to Shell Scripting and need your help in the below situation. - I have two files (File 1 and File 2) and the contents of the files are mentioned below. - "Application handle" is the common field in both the files. (NOTE :- PLEASE REFER TO THE ATTACHMENT "Compare files... (2 Replies)
Discussion started by: Santoshbn
2 Replies

6. UNIX for Dummies Questions & Answers

compare two files based on common field in unix

I have two files in UNIX. 1st file is Entity and Second File is References. 1st File has only one column named Entity ID and 2nd file has two columns Entity ID | Person ID. I want to produce a output file where entity id's are matching in both the files. Entity File 624197 624252 624264... (4 Replies)
Discussion started by: PRS
4 Replies

7. UNIX for Dummies Questions & Answers

find common lines using just one column to compare and result with all columns

Hi. If we have this file A B C 7 8 9 1 2 10 and this other file A C D F 7 9 2 3 9 2 3 4 The result i´m looking for is intersection with A B C D F so the answer here will be (10 Replies)
Discussion started by: alcalina
10 Replies

8. Shell Programming and Scripting

Files common in two sets ??? How to find ??

Suppose we have 2 set of files set 1 set 2 ------ ------ abc hgb def ppp mgh vvv nmk sdf hgb ... (1 Reply)
Discussion started by: skyineyes
1 Replies

9. Shell Programming and Scripting

To find all common lines from 'n' no. of files

Hi, I have one situation. I have some 6-7 no. of files in one directory & I have to extract all the lines which exist in all these files. means I need to extract all common lines from all these files & put them in a separate file. Please help. I know it could be done with the help of... (11 Replies)
Discussion started by: The Observer
11 Replies
Login or Register to Ask a Question