I have got two files of same numer of lines in each.





Now I want to compare them line by line, first line with first line, second with second and so on and print those lines in the first file (1.txt) which are not matching with the corresponding lines in the secon file (2.txt).

In this scenario the output will be:


With awk:

awk 'NR==FNR{a[$0];next}(!($0 in a)){print}' file2 file1

NR==FNR{a[$0];next} :
For the first file (file2) create an array a, with key $0 = whole line , and go to the next line. NR is the global record number of awk and FNR is the current file record number. The condition NR==FNR will be valid only for the first file read (file2)

(!($0 in a){print}:
When the first file2 finishes, then read file1 and if the whole line $0 of file1 does not belong in the keys of array a print this line.

With grep:

grep -vxF -f file2 file1

-v : catch the non matching lines
-x : match the whole line
-F : make a fixed match instead of pattern match
-f : use file2 to load patterns

For the updated data in order to perform a line by line comparison you can use awk and not grep :

awk 'NR==FNR{a[FNR]=$0;next}$0!=a[FNR]{print}' file2 file1

Obviously, both files should have the same number of lines.

  • I went with the second one, using grep and it helped, thank you. – Sayan Sikder Jul 11 '17 at 10:40
  • @SayanSikder grep is all times classic, but my favorite one is awk. – George Vasiliou Jul 11 '17 at 10:56
  • @SayanSikder Since comments are hard to read i propose to include those new lines in your main question (press edit just bellow your existing question) – George Vasiliou Jul 11 '17 at 11:16
  • I have changed the sample inputs, and accordingly the output in the main question. I wanted line to line comparison, each line should match the corresponding lines only of the other file, not just any line of it. – Sayan Sikder Jul 11 '17 at 11:26
  • @SayanSikder See update. Ony awk (or similar advanced text processing language) seems able for this task. grep and probably comm,join,diff i doubt that can match your updated requirement. – George Vasiliou Jul 11 '17 at 11:37

Simple comm (compare two sorted files line by line) approach:

comm -2 -3 1.txt 2.txt

The output:


  • -2 - suppress column 2 (lines unique to FILE2)

  • -3 - suppress column 3 (lines that appear in both files)

If input files are unsorted use the following:

comm -2 -3 <(sort 1.txt) <(sort 2.txt)


The alternative solution using diff tool with --LTYPE-line-format=LFMT options:

diff --unchanged-line-format="" --old-line-format="%L" --new-line-format="" 1.txt 2.txt
  • --unchanged-line-format="" - eliminate printing common lines

  • --new-line-format="" - eliminate printing lines from the 2nd file

  • --old-line-format="%L" - output the crucial lines from the 1st file


Just simply use diff command:

 diff --suppress-common-lines -n file1 file2

For your example files output will look like this:

>diff file1 file2
< 1,2,1,2
< 1,2,3,3
> 1,2,1,1
> 1,2,3,1

< indicates lines from file1,

> indicated lines from file2,

= indicated lines common for both files

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.