Remove newline depending on the format of the next line

前端 未结 4 901
抹茶落季
抹茶落季 2021-01-14 08:34

I have a special file with this kind of format :

title1
_1 texthere
title2
_2 texthere

I would like all newlines starting with \"_\" to be

4条回答
  •  感动是毒
    2021-01-14 09:16

    Try following solution:

    In sed the loop is done creating a label (:a), and while not match last line ($!) append next one (N) and return to label a:

    :a
    $! {
      N
      b a
    }
    

    After this we have the whole file into memory, so do a global substitution for each _ preceded by a newline:

    s/\n_/ _/g
    p
    

    All together is:

    sed -ne ':a ; $! { N ; ba }; s/\n_/ _/g ; p' infile
    

    That yields:

    title1 _1 texthere
    title2 _2 texthere
    

提交回复
热议问题