Compare two files and print the two lines with difference


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare two files and print the two lines with difference
# 8  
Old 06-21-2009
Hi.
Quote:
Originally Posted by kingpeejay
In comparing two files, does it need that the two files have the same number of lines?
If you are asking about combine, then no, the files need not have the same number of lines -- see example below. Your sample had the same number of lines in both files, so that is what I used in my first response. When you are posting sample data, it is best to ensure that all important characteristics are represented.

Note that not only are the number of lines different, but the order can be different -- the lines red and orange, for example ... cheers, drl
Code:
#!/usr/bin/env bash

# @(#) s2	Demonstrate combine from Linux package moreutils.

echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) combine
set -o nounset
echo

FILE1=data3
FILE2=data4

echo
echo " Data file $FILE1, $(wc -l <$FILE1) lines:"
cat $FILE1

echo
echo " Data file $FILE2, $(wc -l <$FILE2) lines:"
cat $FILE2

echo
echo " Results not in $FILE2:"
combine $FILE1 not $FILE2

echo
echo " Results not in $FILE1:"
combine $FILE2 not $FILE1

exit 0

producing:
Code:
% ./s2

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
GNU bash 3.2.39
combine - ( /usr/bin/combine Jun 28 2008 )


 Data file data3, 10 lines:
pink
orange
red
yellow
green
blue
indigo
violet
black
mauve

 Data file data4, 7 lines:
silver
red
orange
yellow
green
blue
violet

 Results not in data4:
pink
indigo
black
mauve

 Results not in data3:
silver

# 9  
Old 06-21-2009
Sorry, this is my revision:
File 1:
abc 123 def
ace 246 gik
xyz 357 vnc
File 2:
jgm 342 tpm
ace 246 gik
abc 321 def
xyz 357 vnc

The output file should be:
"Lines that contains mismatch"
abc 123 def
"This is the correct line"
abc 321 def
# 10  
Old 06-21-2009
That's not the same question!

What do you want?

Lines where the 2nd column is not the same? Lines where the lines are nearly the same but not quite? Or in the case of your example, lines which are NOT the same (which is what you asked originally).

Last edited by Scott; 06-21-2009 at 01:14 PM..
# 11  
Old 06-21-2009
Im sorry. I thought your files doesnt need to be with the same number of lines. My fault.

---------- Post updated at 03:16 AM ---------- Previous update was at 12:04 AM ----------

1. The number of lines on both files is not equal.
2. The difference occurs on the 2nd or 3rd column of the line.
3. The comparison is not between line 1 of file 1 and line 1 of file 2. The comparison is between line 1 of file 1 and the line on file 2 that has the same first word as the line in file 1.

I know it's kinda complicated. Im sorry for the misunderstanding. Thanks!
# 12  
Old 06-22-2009
Quote:
Originally Posted by kingpeejay
...
3. The comparison is not between line 1 of file 1 and line 1 of file 2. The comparison is between line 1 of file 1 and the line on file 2 that has the same first word as the line in file 1.
...
So what should the output be if the files have data as follows ?

Code:
$ 
$ cat file1
abc 123 def
ace 246 gik
xyz 357 vnc
pqr 789 stu
$ 
$ cat file2
jgm 342 tpm
ace 246 gik
abc 321 def
xyz 357 vnc
$ 
$

Quote:
The output file should be:
"Lines that contains mismatch"
abc 123 def
"This is the correct line"
abc 321 def
(a) What makes "abc 321 def" the correct line ?
(b) Why is "abc 123 def" not the correct line ?

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare two variables and print the difference

compare two variables and print the difference i have two variables X1=rac1,rac2 Y1=rac2,rac3 output=rac1,rac3 Use code tags to wrap code fragments or data samples. (1 Reply)
Discussion started by: jhonnyrip
1 Replies

2. Shell Programming and Scripting

Compare two variables and print the difference

Hi PRIM_SEQ=`some sql code` and output of PRIM_SEQ is like below 120 130 STB_SEQ=`some sql code` and output of STB_SEQ is like below 115 110 i need to compare this two variables output ( decimal numbers) 1) What I want to do is to compare every number in the PRIM_SEQ with... (8 Replies)
Discussion started by: amar1208
8 Replies

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

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

5. Shell Programming and Scripting

Compare multiple files and print unique lines

Hi friends, I have multiple files. For now, let's say I have two of the following style cat 1.txt cat 2.txt output.txt Please note that my files are not sorted and in the output file I need another extra column that says the file from which it is coming. I have more than 100... (19 Replies)
Discussion started by: jacobs.smith
19 Replies

6. UNIX for Dummies Questions & Answers

Compare 2 files print the lines of file 2 that contain a string from file 1

Hello I am a new unix user, and I have a work related task to compare 2 files and print all of the lines in file 2 that contain a string from file 1 Note: the fields are in different columns in the files. I suspect the is a good use for awk? Thanks for your time & help File 1 123 232 W343... (6 Replies)
Discussion started by: KevinRidley
6 Replies

7. Shell Programming and Scripting

Compare two columns in two files and print the difference

one file . . importing table employee 119 . . importing table jobs 1 2nd file . . importing table employee 120 . . importing table jobs 1 and would like... (2 Replies)
Discussion started by: jhonnyrip
2 Replies

8. Shell Programming and Scripting

Compare selected columns from a file and print difference

I have learned file comparison from my previous post here. Then, it is comparing the whole line. Now, i have a new problem. I have two files with 3 columns separated with a "|". What i want to do is to compare the second and third column of file 1, and the second and third column of file 2. And... (4 Replies)
Discussion started by: kingpeejay
4 Replies

9. Shell Programming and Scripting

awk to compare lines of two files and print output on screen

hey guys, I have two files both with two columns, I have already created an awk code to ignore certain lines (e.g lines that start with 963) as they wou ld begin with a certain string, however, the rest I have added together and calculated the average. At the moment the code also displays... (3 Replies)
Discussion started by: chlfc
3 Replies

10. Shell Programming and Scripting

to compare two files and to print the difference

suppose one file P1168S P2150L P85L Q597R R1097C Another file P2150L P85L Q597R R1097C R1379C R1587K Then output shud be R1379C R1587K thanks (5 Replies)
Discussion started by: cdfd123
5 Replies
Login or Register to Ask a Question