sed replace line with capture groups

后端 未结 2 1465
甜味超标
甜味超标 2021-01-13 12:33

Is it possible with sed to replace a line with capture groups from the regex?

I have this regex, please note that this is fixed, I cannot change it.

si

相关标签:
2条回答
  • 2021-01-13 13:05

    Perl to the rescue!

    perl -ne 'if (/(.*)simple sample (.*) with (.*)/) {
                print "$1$2\n";
              } else { print }'
    
    0 讨论(0)
  • 2021-01-13 13:12

    Like this?

    echo "This is just a simple sample line with some text" | \
      sed 's/simple sample \(.*\)/\n\1/;s/.*\n//'
    

    The idea is simple: replace the whole regexp match with the captured group preceded by a newline. Then replace everything up to and including the first newline with nothing. Of course, you could use a marker other than the newline.

    0 讨论(0)
提交回复
热议问题