Search code examples
awksedgrepdata-extraction

To print un-matching strings when compared from two files awk/grep/sed


There are two files file1 and file2 both the files contains some similar data. where file1 has some extra data which is not present in file2. i am trying to print those extra data

below awk solution will print only matching data from file1 and file2. i need something that data which is present in file1 but not in file2.

awk 'NR==FNR{patts[$1]=$2;next}{for (i in patts) if (($0 ~ i) && ($0 ~ patts[i])) print}' file2 file1 

file1

papaya
apple
Moosumbi
mango
jackfruit
kiwi
orange
strawberry
banana
grapes
dates

file2

apple
mango
kiwi
strawberry

expected result:-

papaya
Moosumbi
jackfruit
orange
banana
grapes
dates

Solution

  • The command diff is made to that purpose. Just issue:

    diff --changed-group-format='%<' --unchanged-group-format='' file1 file2
    

    and you will get the expected result:

    papaya
    Moosumbi
    jackfruit
    orange
    banana
    grapes
    dates