我只能显示我想要的结果



我有数据

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

您需要使用LookAhead和LookBehind Regex表达式

grep -oP '(?<=;)w*(?=")'

我认为您在Regexr周围玩,以了解有关正则表达式的更多信息。结帐他们的作弊表。

编写所需的表达式的一种更可读的方法是:

grep -oP '(?<=;).*(?=")' file

,将为您带来理想的结果。Perl Regexes显然是实验性的,但某些模式无问题。

正在使用以下选项:

-o --only-matching to the print only the matched parts of a matching line
-P --perl-regexp

使用?=;将为您提供以>; 开头的字符串,但是使用>,您可以在索引之后启动。同样,指定了端字符串标签。

建议其他阅读。

相关内容

最新更新