Regex 使用perl高效地提取第三列
这段Perl代码给出了三个输出Regex 使用perl高效地提取第三列,regex,perl,Regex,Perl,这段Perl代码给出了三个输出 echo one two three four | perl -pe 's/(\w+)\s(\w+)\s(\w+).*/$3/' 有什么优雅的解决方案可以缩短这个冗长的重复正则表达式吗?您可以使用区间quantify{m,n}来指示重复: $ echo one two three four | perl -pe 's/((\w+)\s){2}(\w+).*/$3/' three 因此,对于第100列,您需要: $ perl -pe 's/((\w+)\s){9
echo one two three four | perl -pe 's/(\w+)\s(\w+)\s(\w+).*/$3/'
有什么优雅的解决方案可以缩短这个冗长的重复正则表达式吗?您可以使用区间quantify
{m,n}
来指示重复:
$ echo one two three four | perl -pe 's/((\w+)\s){2}(\w+).*/$3/'
three
因此,对于第100列,您需要:
$ perl -pe 's/((\w+)\s){99}(\w+).*/$100/'
但是,我不建议您这样做,因为有一种更简单的方法可以打印单个字段,即使用cut
:
$ echo one tow three fout | cut -f3 -d' '
three
您可以使用间隔quantify
{m,n}
来指示重复:
$ echo one two three four | perl -pe 's/((\w+)\s){2}(\w+).*/$3/'
three
因此,对于第100列,您需要:
$ perl -pe 's/((\w+)\s){99}(\w+).*/$100/'
但是,我不建议您这样做,因为有一种更简单的方法可以打印单个字段,即使用cut
:
$ echo one tow three fout | cut -f3 -d' '
three
Perl有一个autosplit命令行选项()
编辑:添加了
-l
pervijayPerl有一个autosplit命令行选项()
编辑:根据Vijay添加了-l
为什么不使用:
cut -f3 -d" "
或
或
为什么不使用:
cut -f3 -d" "
或
或
您可以通过添加-l标志
perl-lane'print$F[2]'删除“\n”
您可以通过添加-l标志perl-lane'print$F[2]'删除“\n”
我相信-p和-n是互斥选项(-p覆盖-n)。我相信-p和-n是互斥选项(-p覆盖-n)。