[英]How to match and remove whole lines from a file in ruby?
我有兩個文件:
這兩個文件在每一行都有一個短語,如下所示:
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.