我得到了一个 Perl one-liner。它具有以下形式:
perl -pe'...'
Run Code Online (Sandbox Code Playgroud)
如何指定要处理的文件到程序?
perl在perlrun手册页中可以找到有关如何启动的文档。
perl -pe'...' -i~ file [file [...]] # Modifies named file(s) in place with backup.
perl -pe'...' -i file [file [...]] # Modifies named file(s) in place without backup.
perl -pe'...' file.in >file.out # Reads from named file(s), outputs to STDOUT.
perl -pe'...' <file.in >file.out # Reads from STDIN, outputs to STDOUT.
Run Code Online (Sandbox Code Playgroud)
如果文件名可以以 a 开头-,则可以使用--.
perl -pe'...' [-i[~]] -- "$file" [...]
Run Code Online (Sandbox Code Playgroud)
如果要修改多个文件,可以使用以下任何一种:
find ... -exec perl -pe'...' -i~ {} + # GNU find required
find ... | xargs -r perl -pe'...' -i~ # Doesn't support newlines in names
find ... -print0 | xargs -r0 perl -pe'...' -i~
Run Code Online (Sandbox Code Playgroud)
在上述所有内容中,方括号 ( []) 表示可选内容。它们不应出现在实际命令中。在另一方面,{}中-exec条款应显示为-是。
注意:一些单行使用-n和显式打印而不是-p. 以上所有内容也适用于这些。