I am trying to extract 2 strings from a multiline text.


This line finds some match at the beginning of the string and puts it in a variable inCol.

inCol=$(perl -e 'print $1 if $ARGV[0]=~/^(\\033\[.*?m).*$/mi; ' -- "${postmsg}")

I also want to grab what is the rest of the string (basically the .*$) I tried with this:

inVal=$(perl -e 'print $1 if $ARGV[0]=~/^\\033\[.*?m(.*)$/mi; ' -- "${postmsg}")

This actually works if my input is a one line string. If not, then nothing comes out.

.*$ matches to end of line... tried with \Z but nothing. tried with .* alone, and nothing.

Tried with various modifiers etc... This must be very simple but I cannot find the answer.

Even better, if this could be done in a single line. instead of running the match twice.

2 Answers 2


With bash:


The first removes the longest suffix pattern \n*, the second removes the shortest prefix pattern *\n.


and of course it was simple.

inVal=$(perl -e 'print $2 if $ARGV[0]=~/^(\\033\[.*?m)(.*)$/mis;' -- "${1}")

missing the 's' at the end

  • 1
    Why downvote? Still interested if this can be done in a single line.
    – conanDrum
    Jun 10, 2019 at 22:14

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.