how to combine the below sed commands in Linux

sed -rn 's/.*MsgFlow="([^"]*)".*/\1/p'  ADVQCC_OUTPUT_BRK1


sed -n 's:.*<ProcessDtm>\(.*\)</ProcessDtm>.*:\1:p' ADVQCC_OUTPUT_BRK1


  • 1
    Can you show input file and desired output? – Egor Vasilyev Nov 30 '17 at 10:11
  • 2
    looks like you are trying to parse xml/html content. Post your input file content. sed is NOT a proper tool for such case – RomanPerekhrest Nov 30 '17 at 10:32

I think the more closer variant of two command combination will be (POSIXly):

sed -ne 's/.*MsgFlow=\"\([^"]*\)\".*/\1/p' -ne 's/.*<ProcessDtm>\\\(.*\\\)<\/ProcessDtm>.*/\1/p' ADVQCC_OUTPUT_BRK1

Or simpliest:

sed -e 's/.*MsgFlow=\"\([^"]*\)\".*/\1/' -e 's/.*<ProcessDtm>\\\(.*\\\)<\/ProcessDtm>.*/\1/' ADVQCC_OUTPUT_BRK1

But i need input example to get more exact result.

  • Why are you escaping double quotes inside the single quote script? Why those `\\`? – Philippos Nov 30 '17 at 11:28

Usually, you can simply concatenate sed commands like several s commands with an embedded semicolon:

sed 's/some/another/;s/stuff/thing/'

In your case, one command uses extended regular expressions (ERE), the other basic regular expressions, so you need to convert one or the other:

sed -n 's/.*MsgFlow="\([^"]*\)".*/\1/p;s:.*<ProcessDtm>\(.*\)</ProcessDtm>.*:\1:p' ADVQCC_OUTPUT_BRK1

or with ERE:

sed -En 's/.*MsgFlow="([^"]*)".*/\1/p;s:.*<ProcessDtm>(.*)</ProcessDtm>.*:\1:p' ADVQCC_OUTPUT_BRK1

Remark: I changed -r to -E for selecting ERE, because this is understood by more versions of sed.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.