Remove newline depending on the format of the next line

前端 未结 4 884
抹茶落季
抹茶落季 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条回答
  •  萌比男神i
    2021-01-14 09:19

    If your whole file is like your sample (pairs of lines), then the simplest answer is

    paste - - < file
    

    Otherwise

    awk '
        NR > 1 &&  /^_/ {printf "%s", OFS} 
        NR > 1 && !/^_/ {print ""} 
        {printf "%s", $0} 
        END {print ""}
    ' file 
    

提交回复
热议问题