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+:"?
This might work for you (GNU sed):
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.Alternate awk solution.
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'
something like this?
handle the spaces:
output: