gpt4 book ai didi

ruby - Perl 替代在 Ruby %x{} 中使用时未询问时插入垃圾

转载 作者:太空宇宙 更新时间:2023-11-03 17:37:14 24 4
gpt4 key购买 nike

所以我正在运行这个类轮:

 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>

不确定发生了什么。我保留新行的原因是因为上次我在“查找”字符类中没有换行的情况下运行它时,我得到的只是一排非常非常长的数字。

如果有任何不同,我将使用 %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}

最佳答案

Ruby 的 %x{} 运算符应用类似语义的双引号,这意味着反斜杠是特殊的。将 perl 代码更改为:'s|[^\\d\\n]||g'(在每个有 1 个反斜杠的地方使用 2 个反斜杠)它应该可以工作。

关于ruby - Perl 替代在 Ruby %x{} 中使用时未询问时插入垃圾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13076815/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com