Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find uncommon lines between two text files using shell script?

I have two text files file1.txt & file2.txt

file1.txt Contains :

                 a
                 b
                 c

file2.txt Contains :

                 a
                 b
                 c
                 d
                 e 
                 f

The Output Should be :

                  d
                  e
                  f

The command i'm trying to use is 'diff file2.txt file1.txt' It gives the common lines only.

like image 248
Danish Zahid Malik Avatar asked May 02 '26 03:05

Danish Zahid Malik


2 Answers

Assuming that the input files are sorted:

join -v 2 file1.txt file2.txt

Check man join for details on all the other things join can do for you.

like image 81
DevSolar Avatar answered May 03 '26 18:05

DevSolar


please try below ones

grep -vf file1.txt file2.txt

comm -13 file1.txt file2.txt

for diff you have to perform something extra

diff inp inp1 | grep '>' | cut -f2 -d' '
like image 29
Shravan Yadav Avatar answered May 03 '26 16:05

Shravan Yadav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!