[英]Ruby: how to match a double quote in a regexp
I am trying to remove some double quotes (") characters from a text file using a Ruby one liner, with little success. 我试图使用Ruby one衬里从文本文件中删除一些双引号(“)字符,但收效甚微。
I have tried the following, and some variations, without success. 我尝试了以下内容和一些变体,但均未成功。
ruby -pe 'gsub(/\"/,"")' < myfile.txt
This gives me the following error: 这给了我以下错误:
-e:1: Invalid argument - < (Errno::EINVAL)
I am running Ruby on a Win machine: 我在Win机器上运行Ruby:
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32] 红宝石1.8.6(2007-09-24补丁程序级别111)[i386-mswin32]
Any idea? 任何想法?
Looks like cmd quoting hell -- note that single quotes are meaningless in the cmd shell. 看起来像cmd引用地狱-请注意,在cmd shell中单引号是没有意义的。
ruby -pe "gsub(34.chr,'')" < filename
but this is probably better: 但这可能更好:
ruby -pe "$_.delete!(34.chr)" < filename
怎么样:
ruby -e 'puts $stdin.read.gsub(34.chr,"")' <myfile.txt
ruby -pe 'gsub(/\"/,"")' myfile.txt
Sounds like the problem is with the shell. 听起来问题出在外壳上。
Your error message is from Ruby, so it seems Ruby is receiving the <
as an argument. 您的错误消息来自Ruby,因此看来Ruby正在接收<
作为参数。 This means the shell isn't doing any redirection. 这意味着外壳不执行任何重定向。
I don't have a Windows machine handy so I'd double check that you're getting the redirection right first. 我没有Windows机器,所以我会仔细检查您是否首先正确地完成了重定向。 On first inspection I think the < myfile.txt
should be <myfile.txt
第一次检查时,我认为< myfile.txt
应该是<myfile.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.