当我想要替换许多文件中的常用单词时,我在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 ABCThis 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
更多推荐
发布评论