繁体   English   中英

如何在ruby中匹配并删除文件中的整行?

[英]How to match and remove whole lines from a file in ruby?

我有两个文件:

  1. Phrases.txt
  2. 短语到exclude.txt

这两个文件在每一行都有一个短语,如下所示:

some phrase 1
some phrase 2
some phrase 3

有没有办法可以删除主要“短语”文件中“短语排除”文件中的所有行?

这些短语是准确的 - 因此只需要匹配整行。

phrases = File.read('Phrases.txt').lines
exclude = File.read('Phrases-to-exclude.txt').lines

File.write('Result.txt', (phrases - exclude).join)

你也可以这样做:

data_from_main_file = File.readlines('Phrases.txt')
data_to_exclude = File.readlines('Phrases-to-exclude.txt')
File.write('output.txt', (data_from_main_file - data_to_exclude).join)

暂无
暂无

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

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