Bartosz Kozak
Bartosz Kozak

Reputation: 11

How to pipeline cat to sed pattern

I want to pipline cat output of file to a sed pattern. Suppose I have 2 files: color.txt and duplicate.txt. color.txt contain 3 colors: blue, green, red and duplicate.txt contains green. I want to use sed to delete green from color.txt file.

I try something like this:

cat duplicate.txt | sed "/$/d" < color.txt

Unfortunately, it doesn't work. Any suggestions how to do this?

Upvotes: 0

Views: 3731

Answers (3)

Mark Setchell
Mark Setchell

Reputation: 207668

I think you mean this:

sed "/$(cat duplicate.txt)/d" color.txt

However, as you can tell from the comments, this approach is not very advisable. I urge you to use one of the other solutions. I won't delete this answer though, as it does show the technique you wanted to learn.

Upvotes: 0

aragaer
aragaer

Reputation: 17858

While in this case the grep solution is enough, sometimes I use a command like this:

sed 's|.*|/^\1$/d|' duplicate.txt | sed -i -f - color.txt

Here the first sed invocation converts each line from duplicate.txt into a proper sed command to delete exactly what needed. This results in a sed script that is then passed to second sed using -f - where the - file name means standard input.

Upvotes: 2

codeforester
codeforester

Reputation: 43039

You can use grep to achieve what you want:

grep -Fxvf duplicate.txt color.txt
  • -F to treat lines in duplicate.txt as strings rather than patterns
  • -x to match whole lines in color.txt
  • -v to extract lines that don't match
  • -f to pick up strings from duplicate.txt to match against color.txt

This will work irrespective of how many patterns your files have. For extremely large files, you might want to take a look at this post:

Upvotes: 2

Related Questions