Search code examples
bashunixedi

How to do command "grep -oP" in a line that contains special Characters?


How can I grep a line that contains special characters.

as for example I have a file containing this text

ISA^G00^G          ^G00^G          ^G12^G14147844480    ^GZZ^G001165208      ^G160601^G1903^GU^G00401^G600038486^G0^GP^G>~GS^GTX^G14147844480^G001165208^G20160601^G1903^G600038486^GX^G004010VICS~ST^G864^G384860001~BMG^G00^G^G04~MIT^G000000591^GKohl's AS2 Certificate Change June 21, 2016~N1^GFR^GKOHL'S DEPARTMENT STORES~PER^GIC^GEDIMIO@kohls.com^GTE^G262-703-7334~MSG^GAttention Kohl's AS2 trading partners, Kohl's will be changing.

I would like to grep the line under MSG segment

using this command:

grep -oP 'MSG.\K[\w\s\d]*' < filename

Expected Result :

Attention Kohl's AS2 trading partners, Kohl's will be changing.

Actual Result:

Attention Kohl

How will I do it?


Solution

  • Your pattern:

    grep -Po 'MSG.\K[\w\s\d]*'
    

    is matching just Attention Kohl because you have a single quote after that which will not be matched by any of the \w, \s, \d tokens.

    You also have , and . within your desired portion, so you need to match those too. Also, \d is actually a subset of \w so no need for explicit \d.

    So you can do:

    grep -Po 'MSG.\K[\w\s,.'\'']*'
    

    Or if you you just want to match till the end:

    grep -Po 'MSG.\K.*'