user664833
user664833

Reputation: 19515

remove lines in file from stream

cat file_a
aaa
bbb
ccc

cat file_b
ddd
eee
fff

cat file_x
bbb
ccc
ddd
eee

I want to cat file_a file_b | remove_from_stream_what_is_in(file_x)

Result:

aaa
fff

If there is no basic filter to do this with, then I wonder if there is a way with ruby -ne '...'.

Upvotes: 2

Views: 272

Answers (2)

Cary Swoveland
Cary Swoveland

Reputation: 110725

IO.write('file_a', %w| aaa bbb ccc |.join("\n"))     #=> 11
IO.write('file_b', %w| ddd eee fff |.join("\n"))     #=> 11
IO.write('file_x', %w| bbb ccc ddd eee |.join("\n")) #=> 15

From Ruby:

IO.readlines('file_a', chomp: true) + IO.readlines('file_b', chomp: true) -
  IO.readlines('file_x', chomp: true)
  #=> ["aaa", "fff"]  

Upvotes: 1

John1024
John1024

Reputation: 113924

Try:

$ cat file_a file_b | grep -vFf file_x
aaa
fff

-v means remove matching lines.

-F tells grep to treat the match patterns as fixed strings, not regular expressions.

-f file_x tells grep to get the match patterns from the lines of file_x.

Other options that you may want to consider are:

-w tells grep to match only complete words.

-x tells grep to match only complete lines.

Upvotes: 3

Related Questions