Remove newlines between two words

2019-09-21 09:02发布

问题:

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: ...