gobot6
gobot6

Reputation: 133

How to delete lines in file1 based on column match with file2

I have 2 files; file1 and file2. File1 has many lines/rows and columns. File2 has just one column, with several lines/rows. All of the strings in file2 are found in file1. I want to create a new file (file3), such that the lines in file1 that contain the any of the strings in file2 are deleted.

For example,

File1:

Sally ate 083 popcorn

Rick has 241 cars

John won 505 dollars

Bruce knows 121 people

File2:

083

121

Desired file3:

Rick has 241 cars

John won 505 dollars

Note that I do not want to enter the strings in file 2 into a command manually (the actual files are much larger than in the example).

Thanks!

Upvotes: 1

Views: 371

Answers (3)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

awk approach:

awk 'BEGIN{p=""}FNR==NR{if(!/^$/){p=p$0"|"} next} $0!~substr(p, 1, length(p)-1)' file2 file1 > file3

p="" the variable treated as pattern containing all column values from file2

FNR==NR ensures that the next expression is performed for the first input file i.e. file2

if(!/^$/){p=p$0"|"} means: if it's not an empty line !/^$/ (as it could be according to your input) concatenate pattern parts with | so it eventually will look like 083|121|

$0!~substr(p, 1, length(p)-1) - checks if a line from the second input file(file1) is not matched with pattern(i.e. file2 column values)


The file3 contents:

Rick has 241 cars

John won 505 dollars

Upvotes: 1

VIPIN KUMAR
VIPIN KUMAR

Reputation: 3127

Try this -

#cat f1
Sally ate 083 popcorn
Rick has 241 cars
John won 505 dollars
Bruce knows 121 people

#cat f2
083
121

#grep -vwf f2 f1
Rick has 241 cars
John won 505 dollars

Upvotes: 1

cur4so
cur4so

Reputation: 1820

grep suites your purpose better than a line editor

grep -v -f File2 File1 >File3

Upvotes: 1

Related Questions