Perl的-p命令行开关有什么作用?

Bha*_*ath 4 perl

perl -p -i.bak -e 's/search_str/replace_str/g' filename
Run Code Online (Sandbox Code Playgroud)

是什么-p,-i.bak s/以及/g是什么意思?

cod*_*ict 13

  • -p:假设' while (<>) { ... }'循环程序并打印每个处理过的行.
  • -i.bak:更改输入文件(filename)inplace并将文件创建filename.bak为备份.
  • s in s/:标记替代
  • g - 全局替换......第一次更换后不要停止.

  • 你对`-p`的定义缺少`print`语句.你把它与`-n`混淆了吗? (2认同)

Phi*_*ter 8

来自perlrun:

-p

导致Perl假定你的程序周围有以下循环,这使得它迭代文件名参数有点像sed:

  LINE:
    while (<>) {
        ...             # your program goes here
    } continue {
        print or die "-p destination: $!\n";
    }
Run Code Online (Sandbox Code Playgroud)