Require compare command to compare 4 files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Require compare command to compare 4 files
# 1  
Old 08-26-2010
Require compare command to compare 4 files

I have four files, I need to compare these files together.
As such i know "sdiff and comm" commands but these commands compare 2 files together. If I use sdiff command then i have to compare each file with other which will increase the codes.

Please suggest if you know some commands whcih can compare more than 2 files
# 2  
Old 08-26-2010
Hi.

Perhaps:
Code:
NAME
       diff3 - compare three files line by line

could be generalized to handle n files.

However, you'd need to look over the source.

You haven't said much about what you'd like to see. Do you want simply an indication that the files are the same or different? Do you need to know details on a line-by-line basis? Can the file content be reordered?

One could look at the lengths of the files. If they differ in length, then they cannot be the same. If the length is the same, then you could use a check-summing code, say md5sum, to calculate a numeric signature for the files, and then compare all the signatures.

If you want detailed attempts from responders here, then post some representative samples of 4 files, along with how you think the output should be displayed.

Best wishes ... cheers, drl
# 3  
Old 08-27-2010
Actually my need is that any line in the four files should not be same
e.g.
cat File1
neha
pg

cat file2
aru
arin

cat file3
sample
apple

cat file4
apple
neha
juice


Now a line of file4 is same with file3 and file1. Here i need that command should report that line of file4 is same in file3 and file1
As i want every file should be unique

Thanks
# 4  
Old 08-27-2010
Now the request is clear.

below is the "not-clever" way for you. If you need simplify add some loops on it.

Code:
grep -f fileA fileB ;echo "----------"
grep -f fileA fileC;echo "----------"
grep -f fileA fileD;echo "----------"
grep -f fileB fileC;echo "----------"
grep -f fileB fileD;echo "----------"
grep -f fileC fileD;echo "----------"

# 5  
Old 08-27-2010
Hi.

Here is a solution from radoulov in thread https://www.unix.com/shell-programmin...ple-files.html using your data in files "file1" .. "file4":
Code:
#!/usr/bin/env bash

# @(#) s3	Demonstrate identification of common lines.
# Adapted from: radoulov
# https://www.unix.com/shell-programming-scripting/
# 140390-get-common-lines-multiple-files.html
# Post 8

pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }

pl " Results of finding common lines with awk."
pe

awk 'END {
  for (R in rec) {
    n = split(rec[R], t, "/")
    if (n > 1) 
      dup[n] = dup[n] ? dup[n] RS sprintf("\t%-20s -->\t%s", rec[R], R) : \
        sprintf("\t%-20s -->\t%s", rec[R], R)
    }
  for (D in dup) {
    printf "records found in %d files:\n\n", D
    printf "%s\n\n", dup[D]
    }  
  }
{  
  rec[$0] = rec[$0] ? rec[$0] "/" FILENAME : FILENAME
  }' file?

exit 0

producing:
Code:
% ./s3

-----
 Results of finding common lines with awk.

records found in 2 files:

	file3/file4          -->	apple
	file1/file4          -->	neha

cheers, drl
# 6  
Old 08-27-2010
Thanks drl,

Its working fine when my files are named with file.
Can you tell me the procedure/code from where i can give the name of my files as arguments?

thanks once again
priyanka
# 7  
Old 08-27-2010
Hi.

Near the end there is a string
Code:
file?

that gets expanded by the shell to everything in the current directory that begins with file, like file1, file2, file-jump, etc.

So you can replace file? with your list of files. You can try it with some test filenames first, of course.

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Is there a UNIX command that can compare fields of files with differing number of fields?

Hi, Below are the sample files. x.txt is from an Excel file that is a list of users from Windows and y.txt is a list of database account. $ head -500 x.txt y.txt ==> x.txt <== TEST01 APP_USER_PROFILE USER03 APP_USER_PROFILE TEST02 APP_USER_EXP_PROFILE TEST04 APP_USER_PROFILE USER01 ... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

Simple awk command to compare two files and print first difference

Hello, I have two text files, each with a single column, file 1: 124152970 123899868 123476854 54258288 123117283 file 2: 124152970 123899868 54258288 123117283 122108330 (5 Replies)
Discussion started by: LMHmedchem
5 Replies

3. UNIX for Dummies Questions & Answers

Compare data - Match first column and compare second

Hi guys, looking for some help with a way to compare data in two files but with some conditions. example, File 1 consists of site1,10.1.1.1 site2,20.2.2.2 site3,30.3.3.3 File 2 contains site1,l0.1.1.1 site2,50.1.1.1 site3,30.3.3.3 site4,40.1.1.1 I want to be able to match the... (1 Reply)
Discussion started by: mutley2202
1 Replies

4. UNIX for Dummies Questions & Answers

awk command to compare files by column

So I have this issue. I have 4 files. the first one is the master file who has all possible combinations: file 1 - a - b - c - d - e the other three have some of the letters and a number instead of - for example file 2 34 a 5 c file 3 10 b 12 ... (3 Replies)
Discussion started by: Quijotes
3 Replies

5. Shell Programming and Scripting

Compare two files using awk command recursively

I want to compare two files, 1) Compare Each query result. 2) Compare Only first row of the Query output 3) Compare Time (3rd column), First file time is lesser than 2nd file then print the PO_NUM else do nothing. File1: C:\script>call transaction 1OPOP C:\script>Select ID, PO_ID, TIME, DES... (3 Replies)
Discussion started by: Ragu14
3 Replies

6. Shell Programming and Scripting

Compare 2 files using sdiff command output

Hi All, I have written the shell script which does the following : a. Reads the *.cnf file content from the specified input folder path b. Grep's some strings from the *.cnf file and writes the output in result file(*.cnf_result.txt) in output folder c. Now, if we get new version of... (5 Replies)
Discussion started by: Optimus81
5 Replies

7. Shell Programming and Scripting

Tar command to compare

Hi, I need to compare two directories and I'm using the command tar -dvf directory.tar in the directory on other machine. I saw in the result, message like Uid, Gid, Mod time differs, etc... and it is understandable etc/group: Uid differs etc/group: Gid differs etc/rc2.d/K01nrpe: Mod... (2 Replies)
Discussion started by: hiddenshadow
2 Replies

8. Shell Programming and Scripting

awk command to compare a file with set of files in a directory using 'awk'

Hi, I have a situation to compare one file, say file1.txt with a set of files in directory.The directory contains more than 100 files. To be more precise, the requirement is to compare the first field of file1.txt with the first field in all the files in the directory.The files in the... (10 Replies)
Discussion started by: anandek
10 Replies

9. Shell Programming and Scripting

comm command -- Sort and compare two files

Team, I have two files and I am trying to find the lines unique to file1. So i have executed the below command at shell prompt and got the correct results comm -23 <(sort test) <(sort test1) When i run the same command in Bash shell script, i got the correct results. But when i run... (5 Replies)
Discussion started by: forums123456
5 Replies

10. Shell Programming and Scripting

Compare two arrays in sh or compare two fields

I want a soultion to compare two arrays in sh with an easy way.I want a solution to synchrose users between different AIX servers where no NIS is available. All users are meant to be same on all 10 servers. So the approach is to consider first server as master user repository and whatever the users... (0 Replies)
Discussion started by: rijeshpp
0 Replies
Login or Register to Ask a Question

Featured Tech Videos