sed '/^[[:alpha:]]/{$!N;s/\n/ /;}' <<\DATA
NAME_A
12,1
NAME_B
21,2
DATA
OUTPUT
NAME_A 12,1
NAME_B 21,2
That addresses lines beginning with a letter, pulls in the next if there is one, and substitutes a tab character for the newline.
note that the s/\n/<tab>/
bit contains a literal tab character here, though some sed
s might also support the \t
escape in its place
To handle a recursive situation you need to make it a little more robust, like this:
sed '$!N;/^[[:alpha:]].*\n[^[:alpha:]]/s/\n/ /;P;D' <<\DATA
NAME_A
NAME_B
12,1
NAME_C
21,2
DATA
OUTPUT
NAME_A
NAME_B 12,1
NAME_C 21,2
That slides through a data set always one line ahead. If two ^[[:alpha:]]
lines occur one after the other, it does not mistakenly replace the newline, as you can see.
Some kind of a monster) With perl it should be easier
cat file
ba bla bla hyphe-</page>
<page>nated bla bla bla
and the output should look like
bla bla bla</page>
<page>hyphenated bla bla bla
It's GNU sed (in some other sed-s -E option is used for extended regular expressions)
sed -nr '/[[:alpha:]]+-<\/[[:alpha:]]+>$/{
N
s!([[:alpha:]]+)-(</[[:alpha:]]+>)\n(<[[:alpha:]]+>)([[:alpha:]]+)!\2\n\3\1\4!}
p' file
ba bla bla </page>
<page>hyphenated bla bla bla
and the output should look like
bla bla bla</page>
<page>hyphenated bla bla bla
Best Answer
As fra-san mentioned, GNU
sed
introduced a--debug
option which does pretty much what you’re looking for, in version 4.6; so e.g if you run:the output is
I don’t know what distribution you use, but this version of sed (or a later one) is available in Debian 10, in Ubuntu 19.04, and derivatives; it will be available in Fedora 33.