如何使用perl oneliner循环遍历许多文件

use*_*429 3 perl

我试图使用perl衬里在第4行添加一行到许多perl文件.我在用 :

 perl -pi -le 'print "     cell_type = pad;" if $. ==4' *.cell.plt
Run Code Online (Sandbox Code Playgroud)

但这只是改变我目录中的第一个文件,而不是所有文件.如何一次在所有文件中插入行.我试过几种方法,但总是失败.请帮忙.谢谢.

ike*_*ami 7

你只是从一个文件句柄读取,所以只有一行4.幸运的是,有一种方法可以重置$..

perl -i -ple'
    print "     cell_type = pad;" if $. == 4;
    close ARGV if eof;
' *.cell.plt
Run Code Online (Sandbox Code Playgroud)

(注意与... eof不同eof().)

或者,您可以perl为每个文件执行

find -maxdepth 1 -name '*.cell.plt' -type f -exec \
   perl -i -ple'print "     cell_type = pad;" if $. == 4' {} \;
Run Code Online (Sandbox Code Playgroud)