简体   繁体   中英

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) 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 '...' .

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.

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"]  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM