简体   繁体   English

Windows CMD Line中的Perl和Regex

[英]Perl & Regex within Windows CMD Line

Is there anyway to accomplish matching + storing all in one cmd line? 无论如何,要完成匹配+将所有内容存储在一个cmd行中? So instead of saving the matches to an array: ie 因此,而不是将匹配项保存到数组中:

($matches) = $filecontents =~ m/.../g

...the matches would save to a *.txt file? ...匹配项将保存为* .txt文件? I have been experimenting for a couple of days now, and believe that I am close to a solution. 我已经尝试了几天,并且相信我已经接近解决方案。 But a few nuances of Perl and Windows CMD Prompt are preventing me from accomplishing this task. 但是Perl和Windows CMD Prompt的一些细微差别使我无法完成此任务。 Here's what I most recently tried: 这是我最近尝试过的方法:

% perl -p -i.bak -e "m/(?<=")(\d\.\d+)(?=")/g" filename.extension

I am a beginner with the CMD line, and I am running Windows 7 (soon to be switching over to Linux). 我是CMD产品线的初学者,并且正在运行Windows 7(不久将切换到Linux)。 Obviously I need to specify a file to which I can save my matches. 显然,我需要指定一个文件,可以将匹配项保存到该文件中。 The trouble is, this is where my knowledge drops off. 问题是,这是我的知识下降的地方。 Could someone give me a hand with this? 有人可以帮我这个忙吗? Any comments are appreciated. 任何意见表示赞赏。 Thank you! 谢谢!

If I understand correctly, you want to pull out all of the matches from an entire file, and write those results to a separate file. 如果我理解正确,则希望从整个文件中提取所有匹配项,然后将这些结果写入单独的文件中。

This will work if the below results are what you're after. 如果您得到以下结果,这将起作用。 I don't have a Windows box to test on, but this should work (you might have to use double quotes on the outside of the one-liner and escape the ones inside, but I'm not sure. 我没有要测试的Windows框,但这应该可以工作(您可能必须在单面衬套的外部使用双引号并将其转义为内部,但是我不确定。

This one-liner iterates without printing ( -n ) the 'file.txt' file, and prints a match combined with a newline if there is one into the 'results.txt' file via command-line redirection: 这种单行代码无需打印( -n )'file.txt'文件就可以进行迭代,并且如果通过命令行重定向在'results.txt'文件中包含一个换行符,则将匹配结果与换行符一起打印:

perl -ne 'print "$_\n" for m/(?<=")(\d\.\d+)(?=")/g' file.txt > results.txt

Input file: 输入文件:

$ cat file.txt
one
two "9.162"
three one "6.3"
five one six

Output file: 输出文件:

$ cat results.txt
9.162
6.3

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

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