Sunday, March 13, 2022

[SOLVED] To print unmatched values when compared from files using awk/diff/sed/grep

Issue

I was trying to print some un-matching strings from two files, where in my first file is having string with some integer values separated with spaces, also my second file is having some string values which matches with some of string values from first file but that doesn't have any integers specified in-front of it.

using below awk and diff command i was trying to get unmatched data of my first file when compared with second one.

While using awk it will result the contents of first file, Basically it will print contents of last argument passed to the awk command.

awk -F, 'FNR==NR {a[$1];next} !($0 in a)' f2 f1

while using diff it will result the contents of second file, here it will prints contents of first argument passed.

diff --changed-group-format='%<' --unchanged-group-format='' f2 f1

f1

papaya   10
apple    23
Moosumbi 44
mango    32
jackfruit 15
kiwi  60
orange 11
strawberry 67
banana 99
grapes 21
dates 6

f2

apple
mango
kiwi
strawberry

expected result

papaya   10
Moosumbi 44
jackfruit 15
orange 11
banana 99
grapes 21
dates 6

Solution

This is a very common thing to do in awk: read the values from f2 into an array, then while processing f1, only print the lines where the first field does not exist in the f2 array:

awk 'NR == FNR {f2[$1]; next} !($1 in f2)' f2 f1

I was also going to suggest the grep command from @shellter, but he beat me to it (you should add the -w option though, to match whole words).



Answered By - glenn jackman
Answer Checked By - David Goodson (WPSolving Volunteer)