だから私はこのワンライナーを実行しています:
perl -i -pe 's|[^\d\n]||g' country-ids.txt
次のようなファイルのテキストを置き換えるには:
# encoding: utf-8
files_to_change = ["db_owners.txt", "db_vessels.txt"]
files_to_change.each do |file|
text = File.read(file)
,"1")
,"1")
,"2")
,"2")
,"3")
私の目標は、行を元の場所に保ちながら、数字以外のすべての文字の各行を削除することです。
私が望む結果の代わりに、私はこれを得ています:
d
dd
d
d
<blank space>
何が起こっているのかわからない。新しい行を保持している理由は、「検索」文字クラスで改行なしで最後に実行したときに、取得したのは1行の非常に長い数字だけだったためです。
違いがあれば、%x{command}を使用して.rbスクリプトでこれを実行しています。
編集:
これがスクリプト全体です。それでも同じ問題が発生します。理由はわかりません。
%x{cut -f 2 -d/ script-substitute-countries-with-id.rb > countries2.txt}
%x{cut -f 2 db_vessels.txt > countries.txt}
%x{cut -f 3 -d/ script-substitute-countries-with-id.rb > country-ids.txt}
%x{perl -i -pe 's:[^\d]::g' country-ids.txt}
%x{join countries2.txt country-ids.txt > countries2.txt.tmp}
%x{mv countries2.txt.tmp countries2.txt}
%x{cat countries.txt countries2.txt > countries.txt}
%x{uniq countries.txt > countries.txt.tmp}
%x{mv countries.txt.tmp countries.txt}