简体   繁体   English

grep可以只显示我想要的结果

[英]Can grep show only result i want

I have data as this 我有这样的数据

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

How to get only data between ; 如何只获得之间的数据; and " I have tried grep -oP ';.*?"' file and got output : 和“我已经尝试过grep -oP ';.*?"' file并得到输出:

;8pctgRBMwdffgEio"
;8pctgRBwsaNezxio"
;8pctgsvMALNejkio"
;8pctgxdfALNebfio"
;8pwerRBMALNetior"

But my desired output is: 但是我想要的输出是:

8pctgRB Mwdf fgEio
8pctgRBwsaNezxio
8pctgsvMALNejkio
8pctgxdfALNebfio
8pwerRBMALNetior

A much more readable way to write the expression you need is: 编写所需表达式的一种更具可读性的方法是:

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

and will get you the desired result. 并会为您带来理想的结果。 PERL regexes are apparently experimental but certain patterns work without issues. PERL正则表达式显然是实验性的,但是某些模式可以正常工作。

The following options are being used: 使用了以下选项:

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

Using ?=; 使用?=; will get you the string beginning with ; 会以字符串开头; but using the > you are able to start at the index after. 但是使用>您可以从索引之后开始。 Similarly the end string tag is specified. 类似地,指定结束字符串标签。

Here is suggested additional reading . 建议在这里阅读其他内容

You need to use lookahead and lookbehind regex expressions 您需要使用先行和后向正则表达式

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

I consider you play around regexr to learn more about regular expressions. 我认为您可以在regexr周围学习有关正则表达式的更多信息。 Checkout their cheatsheet. 签出他们的备忘单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM