如何在perl脚本中插入awk命令?

dan*_*dan 0 linux perl awk

我想在我的脚本上添加这个awk命令,但一直收到错误.我已经放入""但仍然出错.

system("awk -F"\t" '{ for ( i=1; i<=2; i++ ) { printf "%s\t", $i } printf "\n"; }' myfile file2"};
Run Code Online (Sandbox Code Playgroud)

错误是

找到运算符在host_parse第21行预期的位置,靠近"t"'{for(i = 1; i <= 2; i ++){printf""

未加引号的字符串"a"可能与myfile第58行的未来保留字冲突.

未加引号的字符串"a"可能与myfile第58行的未来保留字冲突.

myfile第21行的语法错误,靠近""awk -F"\"

谢谢.

mob*_*mob 7

使用该system命令最棘手的部分之一是使用引号,以便将正确的命令传递给操作系统.Perl的q//构造对此非常有帮助:

# treat everything between the @...@ as uninterpolated string
system( q@awk -F"\t" '{ for ( i=1; i<=2; i++ ) { printf "%s\t", $i } 
          printf "\n"; }' myfile file2@ );
Run Code Online (Sandbox Code Playgroud)