crossing crossing - 4 months ago 7
Linux Question

remove whole line from file with 2 column from a text file with 1 column in linux

E.G

File A

abc 123

def 456

ghi 789

File B

def

Resultfile

abc 123

ghi 789

I tried it with sed, grep but it just won't work. I just stated learning linux and coudn't find anything similiar.

Thank you

//*-----------

grep -wvf worked but now i see that i have a problem with sting where a "#" is in front. those will be removed too. By modifying to grep -wxvf, the command won't work at all. Do i need another command other than grep?

Answer
awk 'FNR==NR{a[$0];next}(!($1 in a)){print}' fileb filea

{a[$0];next}- this block executes until FNR==NR(which means for all lines of fileb). "next" ensures that no code gets executed after this block.

At the end of fileb line you have a associative array with key as lines in filb and value as null.

Then processing of lines in filea starts.

{print} will be executed for all lines in filea but on condition (!($1 in a)) which means print the lines in filea only if first field of filea is existing as a key in associative array a