Help with diff command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with diff command
# 1  
Old 04-01-2014
Help with diff command

Platform :Oracle Linux 6.4
Shell : bash

In the below sample, although the lines in a.txt and b.txt are jumbled up, there is only one difference : b.txt has an extra line NETHERLANDS

Code:
$ cat a.txt
SPAIN
NORTH KOREA
PORTUGAL
GERMANY
SYRIA
$
$
$ cat b.txt
GERMANY
NORTH KOREA
SPAIN
PORTUGAL
SYRIA
NETHERLANDS
$
$
$ diff a.txt b.txt
1c1
< SPAIN
---
> GERMANY
2a3
> SPAIN
4d4
< GERMANY
5a6
> NETHERLANDS
$

I have two requirements

1. Ignore the difference in the order in which the lines appear. ie. In the above example, ignore the difference
of order of lines and consider only the line NETHERLANDS (the extra line) as the sole difference.

2. If there is a difference between a.txt and b.txt , just print the message 'a.txt and b.txt are different'
If there is no difference between a.txt and b.txt then print 'a.txt and b.txt are same

How can I do the above ?
# 2  
Old 04-01-2014
Code:
diff <(sort file1) <(sort file2) > /dev/null && echo files are the same || echo files are different

This User Gave Thanks to Subbeh For This Post:
# 3  
Old 04-01-2014
Thank You Subbeh.

Two questions:
1. What is the role of less than character in red below ?

2. What is the role of double pipe operater ?

Code:
$ diff <(sort a.txt) <(sort b.txt) || echo 'there is a difference'
1a2
> NETHERLANDS

# 4  
Old 04-01-2014
Quote:
Originally Posted by John K
Thank You Subbeh.

Two questions:
1. What is the role of less than character in red below ?

2. What is the role of double pipe operater ?

Code:
$ diff <(sort a.txt) <(sort b.txt) || echo 'there is a difference'
1a2
> NETHERLANDS

1. The '<' character is used to redirect the output of sort to the diff utility. This is needed if you're not specifying a file directly to diff.

2. || is equal to OR, && is equal to AND. In this case it means that if the diff command was succesfull, echo there is no difference (AND). If the diff command was not succesfull, echo that there is a difference (OR).

Hope this clears it up for you.
This User Gave Thanks to Subbeh For This Post:
# 5  
Old 04-01-2014
Hello John K,

Here is one more approach for same.

Code:
awk 'NR==FNR{a[$1]=$1;next} !($1 in a){print "File " FILENAME " is different from the first in value: " $1}' check_check_file_a.txt check_check_file_b.txt

Output will be as follows.

Code:
File check_check_file_b.txt is different from the first in value: NETHERLANDS



Thanks,
R. Singh
# 6  
Old 04-01-2014
Regarding your solution :
Code:
diff <(sort a.txt) <(sort b.txt) || echo 'there is a difference'

So, basically this is what is happening:

If the exit status of the first command (diff <(sort a.txt) <(sort b.txt)) is not 0 , then the second command (echo 'there is a difference') will be executed

But why should the diff command command give an exit status 1. It executed succesfully so it should be giving an exit status of 0. Right ?

Code:
$ diff <(sort a.txt) <(sort b.txt)
1a2
> NETHERLANDS
$ echo $?
1
$

# 7  
Old 04-01-2014
Have a look at the man pages;
Quote:
Exit status is 0 if inputs are the same, 1 if different, 2 if trouble.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

One-way diff command?

Hello, I am trying to find the different files between multiple directories in Linux, here is a small assumption of what is inside the directories dir1 dir2 dir3 1.txt 1.txt 1.txt 2.txt 3.txt 3.txt 5.txt 4.txt 5.txt 6.txt 7.txt 8.txt I am using the following... (4 Replies)
Discussion started by: Error404
4 Replies

2. UNIX for Dummies Questions & Answers

Re:using the diff command

Hi Guys I have a situation where I would like to use the diff command but I would like to see "number" of differences and than send it through and if statement and than view the difference if greater than 1. Eg. diff file1 file2 > than gives the "number" and I than say - if number >1... (3 Replies)
Discussion started by: Prega
3 Replies

3. UNIX for Dummies Questions & Answers

Diff command of two files

Hi, I use the diff command to compare two files and append this output to a file. I would like to now not only produce the differences but be able to output the total number of changes made, the number of new files added and the number of files deleted, is there I can do this using the diff... (2 Replies)
Discussion started by: cyberfrog
2 Replies

4. Shell Programming and Scripting

diff command help

Hi all diff file1 file 2 command will give us op of diff between two file. But it aslo give its position and sign "<" or ">". I dont want position and sign in op. Only diff of content should be come as op. Kindly help me for this. Regards Jaydeep (1 Reply)
Discussion started by: jaydeep_sadaria
1 Replies

5. UNIX for Dummies Questions & Answers

diff command

Is there any option for the diff command (or maybe an entirely different command) that will give you only the text that differs between two files? When I use diff file1 file2, if any text on that line differs from one file to the next it'll print out the entire line. I'd like to see only the text... (2 Replies)
Discussion started by: red baron
2 Replies

6. AIX

diff command

hello i've two files. how i get the diff between the two files to new file. thanks best regards ariec (3 Replies)
Discussion started by: ariec
3 Replies

7. Shell Programming and Scripting

need help in diff command :

i have 2 file named test1,test2 contents of test1: 1 2 3 --------------------------- contents of test2: 1 2 3 4 5 -------------------------------------------------------- my desired o/p should be: diff test2 test1 4 (5 Replies)
Discussion started by: ali560045
5 Replies

8. Shell Programming and Scripting

diff command

All, How to exclude a directory while diff execution? For ex: To exclude file which we don't want to see diff, we have -x <filename>. Thanks in advance (1 Reply)
Discussion started by: Vichu
1 Replies

9. Shell Programming and Scripting

Diff command problem

Folks, I am Diff'ing 2 identical files..and the result is, it shows all the lines from 2 files (saying nothing is being matched). If I copy the content from 1 of the file and paste in a newly created file and then do the diff, it equals. 2 files are xml files. I've tried many... (4 Replies)
Discussion started by: gvsreddy_539
4 Replies

10. UNIX for Dummies Questions & Answers

diff command

Hi, I have 2 files i would like to have a DIFF command: 1.Marks differences between files or 2.Mentions just the differences Thanks :) (7 Replies)
Discussion started by: gilead29
7 Replies
Login or Register to Ask a Question