简体   繁体   English

从流中删除文件中的行

[英]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) 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 '...' . 如果没有基本的过滤器可以做到这一点,那么我想知道是否有办法使用ruby -ne '...'

Try: 尝试:

$ cat file_a file_b | grep -vFf file_x
aaa
fff

-v means remove matching lines. -v表示删除匹配的行。

-F tells grep to treat the match patterns as fixed strings, not regular expressions. -F告诉grep将匹配模式视为固定字符串,而不是正则表达式。

-f file_x tells grep to get the match patterns from the lines of file_x . -f file_x告诉grep从file_x行获取匹配模式。

Other options that you may want to consider are: 您可能要考虑的其他选项是:

-w tells grep to match only complete words. -w告诉grep仅匹配完整的单词。

-x tells grep to match only complete lines. -x告诉grep只匹配完整的行。

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: 从Ruby:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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