perl - Make sed not buffer by lines -
i'm not trying prevent sed block-buffering! looking not line-buffer.
i not sure if possible @ all.
basically there big difference between behavior of sed
, of cat
when interacting them raw pseudo-terminal: cat
spit inserted characters when receives them on stdin, while sed
in raw mode not.
a thought experiment carried out: given simple sed command such s/abc/zzz/g
, sending stream of input sed 123ab
means sed
at best can provide on standard output characters 123
, because does not yet know if c
arrive , cause result string 123zzz
, while other character have print came in (allowing "catch up", if will). in way it's obvious why cat
respond immediately; can afford to.
so of course that's how work in ideal world sed
's authors cared kind of use case.
i suspect that not case. in reality, through not terribly exhaustive methods, see sed
line buffer no matter (which allows able figure out whether print 3 z's or not), unless tell care matching regexes past/over newlines, in case buffer whole damn thing before providing output.
my ideal solution find sed
spit out all text has finished parsing, without waiting till end of line so. in little example above, instantly spit characters 1
, 2
, , 3
, , while a
, b
being entered (typed), says nothing, till either c
seen (prints zzz
), or other character x
seen, in case abx
printed, or in case of eof ab
printed.
am sol? should incrementally implement perl code features want, or there still chance sort of magically delicious functionality can got through kind of configuration?
see another question of mine more details on why want this.
so, 1 potential workaround on manually establish groups of input "split" across calls sed
(or in case since i'm dealing perl script, perl's regex replacement operators) can sort of manually flushing. cannot achieve same level of responsiveness because require me think through expression describe points @ "buffering" occur, rather having regex parser automatically it.
there tool matches input stream against multiple regular expressions in parallel , acts decides on match. it's not sed. it's lex. or gnu version, flex.
to make demonstration work, had define yy_input
macro, because flex line-buffering input default. no buffering @ stdio
level, , in "interactive" mode, there assumption don't want process less line @ time.
so not portable other versions of lex.
%{ #include <stdio.h> #define yy_input(buf,result,max_size) \ { \ int c = getchar(); \ result = (c == eof) ? yy_null : (buf[0] = c, 1); \ } %} %% abc fputs("zzz", stdout); fflush(stdout); . fputs(yytext, stdout); fflush(stdout); %% int main(void) { setbuf(stdin, 0); yylex(); }
usage: put program file called abczzz.l
, run
flex --always-interactive -o abczzz.c abczzz.l cc abczzz.c -ll -o abczzz ch in b c 1 2 3 ; echo -n $ch ; sleep 1 ; done | ./abczzz ; echo
Comments
Post a Comment