简体   繁体   中英

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 :

;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.

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. Checkout their cheatsheet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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