如何将文件传递给perl脚本处理,并使用heredoc语法对多行perl脚本进行处理?我试过了,但没有成功:
cat ng.input | perl -nae <<EOF
if (@F==2) {print $F[0] . "\t". $F[1] . "\n"} else { print "\t" . $F[0] . "\n" }
EOF
cat ng.input | perl -nae - <<EOF
if (@F==2) {print $F[0] . "\t". $F[1] . "\n"} else { print "\t" . $F[0] . "\n" }
EOF发布于 2013-04-23 09:21:59
确实没有必要使用here-docs。您可以简单地使用多行参数:
perl -nae'
if (@F==2) {
print $F[0] . "\t". $F[1] . "\n"
} else {
print "\t" . $F[0] . "\n"
}
' ng.input干净,比Barmar的更可移植,只使用一个进程,而不是Barmar的三个进程。
请注意,您的代码可以缩减为
perl -lane'unshift @F, "" if @F!=2; print "$F[0]\t$F[1]";' ng.input甚至是
perl -pale'unshift @F, "" if @F!=2; $_="$F[0]\t$F[1]";' ng.input发布于 2013-04-23 08:12:38
使用进程替换:
cat ng.input | perl -na <(cat <<'EOF'
if (@F==2) {print $F[0] . "\t". $F[1] . "\n"} else { print "\t" . $F[0] . "\n" }
EOF
)另外,在perl标记两边加上单引号,这样EOF脚本中的$F就不会扩展为外壳变量。
发布于 2021-11-02 08:52:30
也可以将perl文档作为"-“第一个参数传递给perl:
perl -lna - ng.input <<'EOF'
if (@F==2) {print $F[0] . "\t". $F[1] . "\n"} else { print "\t" . $F[0] . "\n" }
EOFhttps://stackoverflow.com/questions/16158784
复制相似问题