Compare and Differentiate Multiple Files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare and Differentiate Multiple Files
# 1  
Old 08-20-2014
Compare and Differentiate Multiple Files

Im using code below comparing and getting the diff of two files.
But what about multiple files? can you help me guys

Code:
diff -u file1 file2|sed -n '/^---/!{/^-/s/-//p}'
diff -ui file1 file2| sed -n '/^++/!{/^+/s/+//p}'

Example
File1:
Code:
aaa
bbb
ccc
ddd

File2:
Code:
bbb
eee


File3:
Code:
bbb
ccc
ddd

Output.
Code:
File1 File2 File3 has bbb
File 1 doesnt have eee
File 2 doesnt aaa ccc ddd
File3 doesnt have aaa eee

Output:
Code:
Common Line bbb
etc etc . . .

Moderator's Comments:
Mod Comment CODE tags are for sample input and output as well as for code segments.

Last edited by Don Cragun; 08-20-2014 at 02:45 PM.. Reason: Add CODE tags.
# 2  
Old 08-20-2014
Hello,

Could you please try following code.

Code:
awk 'FILENAME==ARGV[1] {a[$1]=$1;next} !($1 in a){p[$1 " NOT in file1"]=$1 " NOT in file1"} FILENAME==ARGV[2] {b[$1]=$1;next} !($1 in b) {p[$1 " NOT in file2"]= $1 " NOT in file2"} FILENAME==ARGV[3] {c[$1]=$1;next} !($1 in c){p[$1 " NOT in file3"]=$1 " NOT in file3"} FILENAME==ARGV[4] {d[$1]=$1;next} !($1 in d){p[$1 " NOT in file1"]=$1 " NOT in file1"} END{for(u in p){print p[u]}}' file*

Output will be as follows.


Code:
aaa NOT in file2
aaa NOT in file3
eee NOT in file1
ddd NOT in file2
ccc NOT in file2

Thanks,
R. Singh
# 3  
Old 08-20-2014
Hi Ravin,
Thank you, but i hope at least it will show what is the common line.
thanks
# 4  
Old 08-21-2014
Try
Code:
echo common line: $(comm -12 file1 file2 | comm -12 - file3)
common line: bbb
coerdtr@RudisPC:/mnt/9/playground$ for i in 1 2 3; do echo file$i doesn\'t have $(sort -u file[1-3] | comm -23 - file$i); done
file1 doesn't have eee
file2 doesn't have aaa ccc ddd
file3 doesn't have aaa eee

# 5  
Old 08-28-2014
Thanks guys...
# 6  
Old 08-28-2014
You can identify identical files using cksum.
Code:
$ find topdir -type f | xargs cksum -n99 | sort | while read c s n
do
 if [ "$c $s" = "$csl" ]
  then
   echo "$csn$c $s $n"
   csn=''
  else
   csn="$c $s $n
"
  fi
 csl="$c $s"
done | pg

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Differentiate 2 files name

Hello All, I have 2 Type of files. 1. MYTEST001_RKP_DORALDO_20150402120000.zip 2. CMP001_STD001_MOGANO_RPSL_20150409_C.zip I can receive these Two type of file at one location. If i receive second type of file CMP001_STD001_MOGANO_RPSL_20150409_C.zip I have to process without... (9 Replies)
Discussion started by: yadavricky
9 Replies

2. Shell Programming and Scripting

How to compare multiple files in UNIX?

Hi, I have below query related to multiple file comparing I have four files i want to compare it and contents of one file will not be presence in 3other files and if any content found then it will print the execution. Can you please help me how to achieve it. (2 Replies)
Discussion started by: soumyamihp
2 Replies

3. 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

4. Shell Programming and Scripting

Compare multiple columns from 2 files

Hi, I need to compare multiple columns from 2 files. I can, for example, have these 2 files: file1: col1, col2, col3,col4 a,1,4,7 b,2,5,8 c,3,6,9file2: col1, col2, col3,col4 a,2,3,2 b,5,7,5 c,1,9,8As a result, I need for example the difference between the columns 2 and 4: col2,... (3 Replies)
Discussion started by: Subbeh
3 Replies

5. Shell Programming and Scripting

Compare multiple files with multiple number of columns

Hi, input file1 abcd 123 198 xyz1:0909090-0909091 ghij 234 999 xyz2:987654:987655 kilo 7890 7990 xyz3:12345-12357 prem 9 112 xyz5:97-1134 input file2 abcd 123 198 xyz1:0909090-0909091 -9.122 0 abed 88 98 xyz1:98989-090808 -1.234 1.345 ghij 234 999 xyz2:987654:987655 -10.87090909 5... (5 Replies)
Discussion started by: jacobs.smith
5 Replies

6. Shell Programming and Scripting

compare multiple files and get the difference

Hi all, i have 50 files .data should be same in these 50 files , so my task is to find the difference. i need a logic , which finds difference between all files and print in output file with file name where it found that difference . i tried below logic , but its not giving me what i want. let... (2 Replies)
Discussion started by: deepakiniimt
2 Replies

7. Shell Programming and Scripting

Script to Compare Multiple Files.

Hello Everyone, I have strange situation and unable to think of a solution. Situation is as follow, FolderA contrain following files, File1 File2 File3 File4 I want to compare as follow compare File1 with File2 --> Result Same/Diffrent compare File1 with File3 --> Result... (1 Reply)
Discussion started by: ZalimJin
1 Replies

8. Shell Programming and Scripting

String compare in multiple files

Hi I have a requirement to process number of files matching a criteria. The resulted file would be processed indivdually looking for a particular string until another one found lines afterwards. Then look for the occurrence of another string in the result count and display/return the result. ... (13 Replies)
Discussion started by: arungn
13 Replies

9. Shell Programming and Scripting

How to differentiate two tar files

Hi All, I am new to this unix stuff.I just have one doubt:suppose i have two tar files and sometimes it happens that when we just check these files from outside these two tar files look same "Eg: ls -lrt drw-r--r-- 1 oasis logadmin 37067 Apr 3 05:48 file1.tar drw-r--r-- 1 oasis ... (7 Replies)
Discussion started by: siri_14
7 Replies

10. Shell Programming and Scripting

Compare multiple columns between 2 files

hello I need to compare 2 text files. File 1 has 2 columns and file 2 has 1 to many. Sample: File 1: 111 555 222 666 333 777 444 755 File 2: 000 110 113 114 844 111 555 999 202 777 865 098 023 222 313 499 065 655 333 011 890 777 433 (15 Replies)
Discussion started by: stevesmith
15 Replies
Login or Register to Ask a Question