0

how could we compare lines in two files using a shell script.

I want to compare line in one file with the line in other.diff will give me all the differences in two files at a time.i want a line in the first file to be compared with all the lines in the second file and get the common lines as the output. with the line numbers where the lines are present in the second file.

Vijay
  • 65,327
  • 90
  • 227
  • 319
  • 1
    ..that depends on how you want to compare and what do you expect as output? the differences, or same lines, or line counts etc., – vpram86 Oct 14 '09 at 13:49
  • With your comment added, it looks like a use case for `grep`. – Dirk Vollmar Oct 14 '09 at 13:58
  • Homework duplicates: http://stackoverflow.com/questions/1559387/script-unix-compare and http://stackoverflow.com/questions/1543422/how-to-compare-two-files-in-shell-script and http://stackoverflow.com/questions/1544058/how-to-replace-a-line-in-the-same-file-by-sed-in-unix-shell-scripting – Dennis Williamson Oct 14 '09 at 14:44

8 Answers8

2

I am not sure exactly what you are asking, but how about:

grep -n -v "`head -1 FILE1`" FILE2

This will give you the numbered lines in FILE2 that do not contain the 1st line in FILE1.

rsp
  • 23,135
  • 6
  • 55
  • 69
1
comm -12 file1 file2

will show the lines common to both file1 and file2

Nick Dixon
  • 865
  • 5
  • 9
0

paste the code below into a file called intersect.sh

while read line  
do
  grep -n "^${line}$" $2;
done < $1

then make sure you have execute permissions on the file

chmod 777 intersect.sh

then run this command, substituting the names of your two files

./intersect.sh nameOfLongerFile nameOfShorterFile 
Derek Nheiley
  • 41
  • 1
  • 6
0
#!/bin/sh
diff $1 $2
Jonathan Feinberg
  • 44,698
  • 7
  • 80
  • 103
  • I want to compare line one file with the other.diff will give me all the differences in two files at a time.i want a line in the first file to be compared with all the lines in the second file and get the common lines as the output. with the line numbers where the lines are present in the second file. – Vijay Oct 14 '09 at 13:52
0

grep -n is the tool to use in your case. As the pattern, you put the line in the first file that you want to find in the second one. Make your pattern start with ^ and end with $ and put quotes around it.

mouviciel
  • 66,855
  • 13
  • 106
  • 140
0

I want to compare line one file with the other.diff will give me all the differences in two files at a time.i want a line in the first file to be compared with all the lines in the second file and get the common lines as the output. with the line numbers where the lines are present in the second file

I think this gets close.

#!/bin/sh
REF='a.txt'
TARGET='b.txt'

cat $REF | while read line
do
  echo "line: $line"
  echo '==========='
  grep -n $line $TARGET
done
Ewan Todd
  • 7,315
  • 26
  • 33
0

This is not elegant at all, but it is simple

let filea be:

foo
bar
baz
qux
xyz
abc

and fileb be:

aaa
bar
bbb
baz
qux
xxx
foo

Then:

while read a ; do
  grep -n "^${a}$" fileb ;
done < filea

Will output:

7:foo
2:bar
4:baz
5:qux
Chen Levy
  • 15,438
  • 17
  • 74
  • 92
0

you can use comm command to compare 2 files which gives the output in a SET operations format like a - b, a intersection b etc.

this gives the common lines in both files. comm -12 <(sort first.txt | uniq) <(sort second.txt | uniq)

Syntax comm [options]... File1 File2

Options -1 suppress lines unique to file1 -2 suppress lines unique to file2 -3 suppress lines that appear in both files

A file name of `-' means standard input.

Venkataramesh Kommoju
  • 1,053
  • 4
  • 13
  • 19