grep 可以只显示我想要的结果吗
Can grep show only result i want
我有这样的数据
tatusx2.atc?beginnum=0;8pctgRB Mwdf fgEio"text1"text4"text
tatqsx3.atc?beginnum=1;8pctgRBwsaNezxio"text2
tatssx4.atc?beginnum=2;8pctgsvMALNejkio"data2
tatksx4.atc?beginnum=1;8pctgxdfALNebfio"text3
tatzsx5.atc?beginnum=3;8pwerRBMALNetior"datac
如何只获取 ; 之间的数据和 ”
我试过 grep -oP ';.*?"' file
并得到输出:
;8pctgRBMwdffgEio"
;8pctgRBwsaNezxio"
;8pctgsvMALNejkio"
;8pctgxdfALNebfio"
;8pwerRBMALNetior"
但我想要的输出是:
8pctgRB Mwdf fgEio
8pctgRBwsaNezxio
8pctgsvMALNejkio
8pctgxdfALNebfio
8pwerRBMALNetior
编写所需表达式的可读性更高的方法是:
grep -oP '(?<=;).*(?=")' file
并且会给你想要的结果。 PERL 正则表达式显然是实验性的,但某些模式可以正常工作。
正在使用以下选项:
-o --only-matching to the print only the matched parts of a matching line
-P --perl-regexp
使用 ?=;
将得到以 ; 开头的字符串,但使用 >
则可以从后面的索引开始。同样指定结束字符串标记。
这里建议additional reading.
您需要使用先行和后行正则表达式
grep -oP '(?<=;)\w*(?=")'
我认为您可以尝试 regexr 以了解有关正则表达式的更多信息。查看他们的备忘单。
我有这样的数据
tatusx2.atc?beginnum=0;8pctgRB Mwdf fgEio"text1"text4"text
tatqsx3.atc?beginnum=1;8pctgRBwsaNezxio"text2
tatssx4.atc?beginnum=2;8pctgsvMALNejkio"data2
tatksx4.atc?beginnum=1;8pctgxdfALNebfio"text3
tatzsx5.atc?beginnum=3;8pwerRBMALNetior"datac
如何只获取 ; 之间的数据和 ”
我试过 grep -oP ';.*?"' file
并得到输出:
;8pctgRBMwdffgEio"
;8pctgRBwsaNezxio"
;8pctgsvMALNejkio"
;8pctgxdfALNebfio"
;8pwerRBMALNetior"
但我想要的输出是:
8pctgRB Mwdf fgEio
8pctgRBwsaNezxio
8pctgsvMALNejkio
8pctgxdfALNebfio
8pwerRBMALNetior
编写所需表达式的可读性更高的方法是:
grep -oP '(?<=;).*(?=")' file
并且会给你想要的结果。 PERL 正则表达式显然是实验性的,但某些模式可以正常工作。
正在使用以下选项:
-o --only-matching to the print only the matched parts of a matching line
-P --perl-regexp
使用 ?=;
将得到以 ; 开头的字符串,但使用 >
则可以从后面的索引开始。同样指定结束字符串标记。
这里建议additional reading.
您需要使用先行和后行正则表达式
grep -oP '(?<=;)\w*(?=")'
我认为您可以尝试 regexr 以了解有关正则表达式的更多信息。查看他们的备忘单。