在perl中执行命令时

编程入门 行业动态 更新时间:2024-10-26 06:28:12
在perl中执行命令时-pi和-e是什么[关闭](What are -pi and -e when executing a command in perl [closed])

当我想要替换许多文件中的常用单词时,我在unix中使用命令行中的perl。 我想知道运行它时-pi和-e是什么,如果我不放这些,会发生什么。

示例: perl -pi -e 's/design/dezine/g' *

When I want to replace a common word in many files I use perl from the command line in unix. I want to know what are the -pi and -e when running it, and what happens if I don't put those.

Example: perl -pi -e 's/design/dezine/g' *

最满意答案

这全部记录在perlrun中 (从perldoc perlrun和man perlrun )。

-e将参数的其余部分(如果有)或下一个参数(否则)视为要执行的Perl代码。 取代脚本名称。

$ perl -e'print "abc\n";' abc

-n为ARGV每一行执行脚本(或-e代码)。 该行将出现在$_ 。

$ perl -MO=Deparse -ne'print uc($_);' LINE: while (defined($_ = <ARGV>)) { print uc $_; } -e syntax OK

$ echo abc | perl -ne'print uc($_);' ABC

-p类似于-n ,除了它还会导致在执行代码后打印$_ 。

$ perl -MO=Deparse -pe'print uc($_);' LINE: while (defined($_ = <ARGV>)) { print uc $_; } continue { die "-p destination: $!\n" unless print $_; } -e syntax OK

$ echo abc | perl -pe'$_ = uc($_);' ABC

-i代表“就地”。 它将输出“重定向”回ARGV正在读取的文件。

BEGIN { $^I = ""; } LINE: while (defined($_ = <ARGV>)) { print uc $_; } continue { die "-p destination: $!\n" unless print $_; } -e syntax OK

$ echo abc >file $ perl -i -pe'$_ = uc($_);' file $ cat file ABC

This is all documented in perlrun (obtained from perldoc perlrun and man perlrun).

-e treat the rest of the argument (if any) or the next argument (otherwise) as Perl code to execute. Takes the place of a script name.

$ perl -e'print "abc\n";' abc

-n executes the script (or -e code) for each line of ARGV. The line will be present in $_.

$ perl -MO=Deparse -ne'print uc($_);' LINE: while (defined($_ = <ARGV>)) { print uc $_; } -e syntax OK

$ echo abc | perl -ne'print uc($_);' ABC

-p is like -n, except it also causes $_ to be printed after the code is executed.

$ perl -MO=Deparse -pe'print uc($_);' LINE: while (defined($_ = <ARGV>)) { print uc $_; } continue { die "-p destination: $!\n" unless print $_; } -e syntax OK

$ echo abc | perl -pe'$_ = uc($_);' ABC

-i stands for "in-place". It "redirects" output back to the file ARGV is reading from.

BEGIN { $^I = ""; } LINE: while (defined($_ = <ARGV>)) { print uc $_; } continue { die "-p destination: $!\n" unless print $_; } -e syntax OK

$ echo abc >file $ perl -i -pe'$_ = uc($_);' file $ cat file ABC

更多推荐

本文发布于:2023-08-04 13:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415625.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令   perl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!