为什么我必须将 perl 输出通过管道传输到新的 perl 命令中
why do I have to pipe perl output into a new perl command
我想在执行许多其他正则表达式替换的 perl 脚本中删除反引号 (\x60) 和单词字符之间的空格。如果我将先前替换的结果打印到 bash shell,然后通过管道传输到新的 perl 调用中,它就可以工作。但在单个 perl 调用中,它不会。在下面的示例中,第三行(单个 perl 命令)不起作用,而第四行(两个单独的命令)起作用。
printf '%b' 'Well, `\n he said it \n'
printf '%b' 'Well, `\n he said it \n' | perl -p -e 'use strict; use warnings; s|\n||g;' ; echo
printf '%b' 'Well, `\n he said it \n' | perl -p -e 'use strict; use warnings; s|\n||g; s|([\x60])\s*(\w)||g;' ; echo
printf '%b' 'Well, `\n he said it \n' | perl -p -e 'use strict; use warnings; s|\n||g;' | perl -p -e 'use strict; use warnings; s|([\x60])\s*(\w)||g;'; echo
为什么它在单个 perl 调用中不起作用?我认为我们应该避免使用多个或嵌套管道 (subshells).
这是 perl 5,版本 18 和 GNU bash,BSD unix 中的版本 3.2.57(1)。
因为您的 perl -p
在换行符上拆分,所以您的第一个 perl 将它们删除,而第二个 perl 将所有内容视为在一行中。
printf '%b' 'Well, `\n he said it \n' | perl -p0 -e 'use strict; use warnings; s|\n||g; s|([\x60])\s*(\w)||g;' ; echo
通过告诉 perl 一次全部吞下,第一个 s
可以删除新行,第二个将删除引号后的空格。
我想在执行许多其他正则表达式替换的 perl 脚本中删除反引号 (\x60) 和单词字符之间的空格。如果我将先前替换的结果打印到 bash shell,然后通过管道传输到新的 perl 调用中,它就可以工作。但在单个 perl 调用中,它不会。在下面的示例中,第三行(单个 perl 命令)不起作用,而第四行(两个单独的命令)起作用。
printf '%b' 'Well, `\n he said it \n'
printf '%b' 'Well, `\n he said it \n' | perl -p -e 'use strict; use warnings; s|\n||g;' ; echo
printf '%b' 'Well, `\n he said it \n' | perl -p -e 'use strict; use warnings; s|\n||g; s|([\x60])\s*(\w)||g;' ; echo
printf '%b' 'Well, `\n he said it \n' | perl -p -e 'use strict; use warnings; s|\n||g;' | perl -p -e 'use strict; use warnings; s|([\x60])\s*(\w)||g;'; echo
为什么它在单个 perl 调用中不起作用?我认为我们应该避免使用多个或嵌套管道 (subshells).
这是 perl 5,版本 18 和 GNU bash,BSD unix 中的版本 3.2.57(1)。
因为您的 perl -p
在换行符上拆分,所以您的第一个 perl 将它们删除,而第二个 perl 将所有内容视为在一行中。
printf '%b' 'Well, `\n he said it \n' | perl -p0 -e 'use strict; use warnings; s|\n||g; s|([\x60])\s*(\w)||g;' ; echo
通过告诉 perl 一次全部吞下,第一个 s
可以删除新行,第二个将删除引号后的空格。