text file: how to remove line break if line does not start with number? (in emacs or sed) -
text file: how to remove line break if line does not start with number? (in emacs or sed) -
i have text file (data base of operations output) many line breaks, i have remove of line breaks (but not of them).
i managed hand, there many lines (thousands), automated solution helpful.
the aim in end have lines in text file start number, other lines shall appended previous line (the lastly started number)
what code should do:
go each line if not start number [0-9] go origin of line(c-a)
, remove linebreak before (like hitting backspace
) (the lines numbers not have leading spaces!) then go next line , same this should quite easy, don't know hot it.
a solution emacs helpful, can within cygwin helpful.
so, goal remove newlines not followed number. 1 way sed:
sed -i ':a $!{n; ba}; s/\n\+/\n/g; s/\n\([^0-9]\)/\1/g' filename
this reads whole line pattern space, replaces sequences of multiple newlines one, , removes newlines aren't followed number.
addendum: ah, cygwin. caveat: code, is, assumes unix line endings. create work on file windows line endings1, may of import you, use
sed -i ':a $!{ n; ba; }; s/\(\r\n\)\+/\r\n/g; s/\r\n\([^0-9]\)/\1/g' filename
this same thing \r\n
instead of \n
. file mixed line endings, use
sed -i ':a $!{ n; ba; }; s/\(\r\?\n\)\+/\n/g; s/\n\([^0-9]\)/\1/g' filename
(or utilize dos2unix
/unix2dos
before processing file.)
1 when file opened unix path or pipes involved , moon gibbous, depending on cygwin settings. handling of line endings in cygwin convoluted , confusing, , in general not bad approach convert file unix line endings before processing cygwin tools, convert afterwards.
emacs sed elisp
Comments
Post a Comment