Remove newlines between two words

2019-09-21 08:54发布

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+:"?

4条回答
爷、活的狠高调
2楼-- · 2019-09-21 09:35

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楼-- · 2019-09-21 09:40

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楼-- · 2019-09-21 09:42

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: ...
查看更多
Root(大扎)
5楼-- · 2019-09-21 09:47

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: ...
查看更多
登录 后发表回答