How can I grep a text piece from a 1 line text file with delimiters

Solution 1:

Thanks to @terdon and @jhilmer for making the quoting decidedly less tricky

If you want the '

$ grep -oE "(STX|IRF).*'" file 
STX=ANAA:1+asdf+5060128703127:P'
IRF=16165193117+160624+160624 '

If you don't want the '

$ grep -oE "(STX|IRF)[^']*"
STX=ANAA:1+asdf+5060128703127:P
IRF=16165193117+160624+160624

Explanation

  • -o just show the matched part
  • -E use ERE so we can use | to search for multiple patterns
  • " start quoting/stop quoting
  • (THIS|THAT) match THIS or THAT
  • .* match any number of any characters
  • \' literal '
  • [^']* any number of any characters except '