Perl 正则表达式从命令行作用于文件
在一个文件中,假设 xyz.txt 我想替换任何数字后跟点的模式,例如:1.,2.,10.,11。等等..有一个空格。 如何在命令行上编写一个 perl 命令来作用于文件以执行上述操作,应该使用什么正则表达式?
请帮忙 谢谢。
In a file, say xyz.txt i want to replace the pattern of any number followed by a dot example:1.,2.,10.,11. etc.. with a whitespace.
How to compose a perl command on the command line to act on the file to do the above, what should be the regex to be used ?
Please Help
Thank You.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这必须是 Perl oneliner 吗?
Perl 命令行选项:
-i
用于指定输入文件发生的情况。如果你不给它一个文件扩展名,原始文件就会丢失并被 Perl munged 输出替换。例如,如果我有这样的:原始文件将以 本身将包含您的输出。
.bak
后缀存储,并且-p
表示将 Perl 程序包含在一个打印循环中,该循环看起来SOMEWHAT如下:这是对正在发生的事情的稍微简化的解释。您可以通过从命令行执行
perldoc perlrun
来查看实际的 perl 循环。主要思想是它允许您像sed
或awk
一样对文件的每一行进行操作。-e
仅包含您的 Perl 命令。您还可以进行文件重定向:
This HAS to be a Perl oneliner?
The Perl command line options:
-i
is used to specify what happens to the input file. If you don't give it a file extension, the original file is lost and is replaced by the Perl munged output. For example, if I had this:The original file would be stored with a
.bak
suffix and <fileName> itself would contain your output.The
-p
means to enclose your Perl program in a print loop that looks SOMEWHAT like this:This is a somewhat simplified explanation what's going on. You can see the actual perl loop by doing a
perldoc perlrun
from the command line. The main idea is that it allows you to act on each line of a file just likesed
orawk
.The
-e
simply contains your Perl command.You can also do file redirection too:
答案(未测试):
Answer (not tested):
都
和
无法在我的系统上执行。
我使用以下顺序:
Both
and
cannot execute on my system.
I use the following order: