newline - sed: Why does command q add a new line? -
suppose following command
echo -en "abc1\ndef2\nghi1" | sed -n 'p; d;' in case output same without sed @ all. last line still has no new line character. next command
echo -en "abc1\ndef2\nghi1" | sed -n '$! {p; d;}; /1$/ {s/1$//; p; d;}' sed prints last line without modification. last line shortened 1 character. still there no new line character on last line. next command
echo -en "abc1\ndef2\nghi1" | sed -n '$! {p; d;}; /1$/ {s/1$//; p; q1;}' ("d" replaced "q1" in last command block. same output before, time there additional new line character in last line.
- why?
- how fix?
(for interested in intention command: given stdin, want scan last character, pass on stdin stdout without last character , set exit code based on character. there should no other modification. sed seems perfect, if there wouldn't newline problem
sed -n ' $! {p; d;}; #print non last line, next cycle /0$/ {s/0$//; p; d}; #last line ending 0? remove 0, print, next cycle /1$/ {s/1$//; p; d}; #last line ending 1? remove 1, print, next cycle {p} #fall back, print last line ' so far script works perfect regarding newline issue. no new line added. if replace "d" command "q"
sed -n ' $! {p; d;}; #print non last line, next cycle /0$/ {s/0$//; p; q0}; #last line ending 0? remove 0, print, exit 0 /1$/ {s/1$//; p; q1}; #last line ending 1? remove 1, print, exit 1 {p} #fall back, print last line ' the newline problem arise...
other solutions welcome, should fast possible.
i guess bug. according manual, q should not print pattern space if auto-print disabled. thus, should not print anything. since using gnu sed, avoid problem using q instead of q. @ least, works me (version 4.2.2).
Comments
Post a Comment