如何像在 awk 中一样在 perl 中替换和打印列

how to substitute and print column in perl just like in awk

我有这个 bash/awk 代码来处理主机

$ echo 'the.pattern to change' | awk '{sub(/the./,"")}{print }'
pattern

我需要用 perl 替换 awk。 知道如何用 perl oneliner 替换语句的第二部分吗?

您可以使用带有拆分功能的Perl one liner。

我们需要使用 the.space 来拆分字符。所以你可以在split函数中使用RegEx。

echo 'the.pattern to change' | perl -aF"/the\.|\s/" -ne 'print $F[1],"\n";'

-a自动拆分模式

echo 'the.pattern to change' | perl -ane '$F[0]=~s/the\.//; print $F[0],"\n";'

怎么样:

echo 'the.pattern to change' | perl -aE '$F[0] =~ s/the\.//; say $F[0]'

输出:

pattern

还有一种方法:

echo 'the.pattern to change' | perl -lpe 's/the\.//;$_=(/[^ ]+/g)[0];'