How to diff to get only matching results?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to diff to get only matching results?
# 1  
Old 10-31-2014
How to diff to get only matching results?

Hello friends,

I have two files that contain data like this:

file1:

Code:
abc, xyx, 123, 998, 324, 320, rex

file 2:

Code:
zoo, rex, too, 332, 004, 047, abc, 123

As you can see both files contain matching patterns but arrangement of these patterns are different. The diff should ignore the arrangement and only see if two matching patterns are found.

How can I diff in a way that I know what matched in both files?

The output should only display the results what is found matching.

Thank you!
# 2  
Old 10-31-2014
How about
Code:
grep -f <(tr ',' '\n' < file1) <(tr ',' '\n' <file2)
 rex
 abc
 123

or even
Code:
grep -of <(tr ',' '\n' < file1) file2
 rex
abc
 123

# 3  
Old 10-31-2014
If by "patterns" you mean individual list items, then diff cannot do this.

Are the patterns on common line numbers e.g., "rex" if it exists in line 1 of FileA, then look for "rex" in line 1 of FileB.

Please give us 2-3 lines of sample input for both files, with expected output.
# 4  
Old 10-31-2014
Follow-on -

Code:
awk 'FILENAME=="FileA" { for(i=1; i<=NF; i++) {arr[$(i)]++ } }
        FILENAME=="FileB" { for(i=1; i<=NF; i++) {if( $(i) in arr ){ print $(i)} }}' 
       '  FileA FileB | sort -u  > newfile

# 5  
Old 10-31-2014
This is what I am trying to accomplish

File1:
Code:
123, zxc, 1123, wed

File2:
Code:
ggf, 123, 329, 1123

I run a diff that gives me this:

Code:
123, 1123
or
1123, 123

Thanks

---------- Post updated at 11:32 AM ---------- Previous update was at 11:28 AM ----------

Hi RudiC,

I ran your first command, is it giving matching output that is common in file 1 and file 2?
# 6  
Old 10-31-2014
DallasT,
the answer to your last question seems to be "it depends", respectively "yes and no". Somehow the order of the files seems to matter (same applies to the second grep approach):
Code:
$ grep -f <(tr ',' '\n' < file1) <(tr ',' '\n' <file2)
 rex
 abc
 123
$ grep -f <(tr ',' '\n' < file2) <(tr ',' '\n' <file1)
 123
 rex
$

If you're working with Linux and GNU sed, following might be a viable solution:
Code:
sed 's/, /\n/g' fileA fileB | sort | uniq -d

# 7  
Old 11-01-2014
I'd say you need to be way more specific when describing what you need to be done.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

I want to add a variable for the results from the formula of one variable and results of another var

Good morning all, This is the file name in question OD_Orders_2019-02-19.csv I am trying to create a bash script to read into files with yesterdays date on the file name while retaining the rest of the files name. I would like for $y to equal, the name of the file with a formula output with... (2 Replies)
Discussion started by: Ibrahim A
2 Replies

2. Shell Programming and Scripting

Diff 3 files, but diff only their 2nd column

Guys i have 3 files, but i want to compare and diff only the 2nd column path=`/home/whois/doms` for i in `cat domain.tx` do whois $i| sed -n '/Registry Registrant ID:/,/Registrant Email:/p' > $path/$i.registrant whois $i| sed -n '/Registry Admin ID:/,/Admin Email:/p' > $path/$i.admin... (10 Replies)
Discussion started by: kenshinhimura
10 Replies

3. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

4. Shell Programming and Scripting

Can ctag and cscope support recording search results and displaying the history results ?

Hello , When using vim, can ctag and cscope support recording search results and displaying the history results ? Once I jump to one tag, I can use :tnext to jump to next tag, but how can I display the preview search result? (0 Replies)
Discussion started by: 915086731
0 Replies

5. Shell Programming and Scripting

serach diff filename in diff location using shell scripting

Hi, I am new to shell scripting. please help me to find out the solution. I need a script where we need to read the text file(consists of all file names) and get the file names one by one and append the date suffix for each file name as 'yyyymmdd' . Then search each file if exists... (1 Reply)
Discussion started by: Lucky123
1 Replies

6. Shell Programming and Scripting

.procmailrc and uudeview (put attachments from diff senders to diff folders)

Moderator, please, delete this topic (1 Reply)
Discussion started by: optik77
1 Replies

7. Shell Programming and Scripting

awk - Matching columns between 2 files and reordering results

I am trying to match 4 colums (first_name,last_name,dob,ssn) between 2 files and when there is an exact match I need to write out these matches to a new file with a combination of fields from file1 and file2. I've managed to come up with a way to match these 2 files based on the columns (see below)... (7 Replies)
Discussion started by: ambroze
7 Replies

8. Shell Programming and Scripting

Awk - Count instances of a number in col1 and put results in a col2 (new) of diff file

I have 2 files as follows: filename1: : 6742 /welcome/mundial98_ahf1_404.htm 1020 6743 /welcome/mundial98_ahf1_404.htm 2224 6744 /welcome/mundial_ef1_404.htm 21678 6745 /welcome/mundial_if_404.htm 4236 6746 /welcome/mundial_lf1_404.htm 21678 filename2: 6746 894694763 1... (2 Replies)
Discussion started by: jontjioe
2 Replies

9. Shell Programming and Scripting

Simulate SVN diff using plain diff

Hi, svn diff does not work very well with 2 local folders, so I am trying to do this diff using diff locally. since there's a bunch of meta files in an svn directory, I want to do a diff that excludes everything EXCEPT *.java files. there seems to be only an --exclude option, so I'm not sure... (3 Replies)
Discussion started by: ackbarr
3 Replies

10. Shell Programming and Scripting

diff 2 files; output diff's to 3rd file

Hello, I want to compare two files. All records in file 2 that are not in file 1 should be output to file 3. For example: file 1 123 1234 123456 file 2 123 2345 23456 file 3 should have 2345 23456 I have looked at diff, bdiff, cmp, comm, diff3 without any luck! (2 Replies)
Discussion started by: blt123
2 Replies
Login or Register to Ask a Question