Robert Schmidt
Robert Schmidt

Reputation: 135

Delete line from a text file that contains any string from another file using sed/awk/etc

I'm a bit of a total beginner when it comes to programming and I appreciate all help you are willing to provide.

Here's my problem...

I have a data.txt file with a lot of lines in it and a strings.txt that contains some strings (1 string per line).

I want to delete all lines from data.txt if they contain any string from strings.txt and to save that new file as proc_data.txt.

I know that I could use sed to search and delete for 1 or more strings but having 500+ strings to type in a CLI makes it ... well, you know.

What I've tried so far

~$ for i in `cat strings.txt`; do sed '/${i}/d' data.txt -i.bak; done

but it just makes a backup of data.txt with the same size.

What am I doing wrong?

Upvotes: 1

Views: 3006

Answers (1)

Birei
Birei

Reputation: 36292

Use grep:

LC_ALL=C fgrep -v -f strings.txt data.txt >proc_data.txt

It searches all strings of strings.txt in data.txt with switch -f. Reverse the result adding -v. Redirect output to your desired file.

Upvotes: 3

Related Questions