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

I want to grep STX= ....... until its first delimiter 'and IRF= ..... until its first delimiter '.

Like:

:STX=ANAA:1+asdf+5060128703127:P'
IRF=16165193117+160624+160624 '
1

1 Answer

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 '
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like