Remove newlines between two words

让人想犯罪 __ 提交于 2019-12-25 18:56:17

问题


I have a file that looks like:

name: charles
key1: how
are
you?
name: erika
key2: I'm
fine,
thanks
name: ...

How could I remove the new lines that between "key\d+" and "name:"? It should look like this:

name: charles
key1: how are you?
name: erika
key2: I'm fine, thanks
name: ...

I'm trying to use sed or awk but without any success. Is there any way to clean that lines?

Is there any way to remove the newlines between "key\d+:" and "\w+:"?


回答1:


something like this?

kent$  awk '/^key.:/{p=1}/^name:/{p=0}
            {if(p)printf "%s",$0;else printf "%s%s\n", (NR==1?"":RS),$0}' file
name: charles
key1: howareyou?
name: erika
key2: I'mfine,thanks
name: ...

handle the spaces:

awk '/^key.:/{p=1}/^name:/{p=0}
    {if(p)printf "%s%s",(/^key.:/?"":" "),$0;
    else printf "%s%s\n", (NR==1?"":RS),$0}' file

output:

name: charles
key1: how are you?
name: erika
key2: I'm fine, thanks
name: ...



回答2:


This might work for you (GNU sed):

sed ':a;$!N;/\n.*:/!s/\n/ /;ta;P;D' file

Keep two lines in the pattern space and remove the newline from the first if the second does not start with a keyword and :. When a keyword and : is encounter print and then delete the first line.




回答3:


Alternate awk solution.

awk '
  {
    if      (NR == 1)                s = ""
    else if ($1 ~ /^[[:alnum:]]+:$/) s = RS
    else                             s = " "
    printf "%s%s", s, $0
  }
  END {print ""}
'<<EOF
name: charles
key1: how
are
you?
name: erika
key2: I'm
fine,
thanks
EOF
name: charles
key1: how are you?
name: erika
key2: I'm fine, thanks



回答4:


The following works, but performance is obviously not the best:

cat file.txt | while read x;do echo "$x" | grep -q ':' && echo; echo -ne "$x "; done | sed -n 's/ $//p'

name: charles
key1: how are you?
name: erika
key2: I'm fine, thanks
name: ...


来源:https://stackoverflow.com/questions/25099382/remove-newlines-between-two-words

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!