unix sed substitute nth occurence misfunction?

前端 未结 3 416
抹茶落季
抹茶落季 2021-01-24 19:07

Let\'s say I have a string which contains multiple occurences of the letter Z. For example: aaZbbZccZ. I want to print parts of that string, each time until the nex

3条回答
  •  一生所求
    2021-01-24 19:41

    This might work for you (GNU sed):

    sed -n 's/Z/&\n/g;:a;/\n/P;s/\n\(.*Z\)/\1/;ta' file
    

    Use sed's grep-like option -n to explicitly print content. Append a newline after each Z. If there were no substitutions then there is nothing to be done. Print upto the first newline, remove the first newline if the following characters contain a Z and repeat.

提交回复
热议问题