regex - SED script not matching single in Multiline pattern after line breaks -
i trying produce sed script converts
&&a_x* &&b_x;cx &&d_x*
into
a_x ax b_x cx d_x dx
a *
should trigger duplication removed _
, ;
simple linebreak.
i have sed script first inserts linebreaks (including operation ;
) , executes multiple line pattern duplication without _
.
the multiple line pattern works if move seperate script file , pipe output of instructions executing linebreaks.
for strange reason single script file won't - want maintenance reasons.
here's combined version:
#!/bin/sed -f # remove whitespaces s/\ //g # linebreak on && s/\&\&/\ \&\&/g ### linebreak on ; s/\;/\ /g # remove new line s/\n// :extendvars /^..*\*$/ { l //debug switch h s/\(\&\|\*\)\(\&\|\*\)*//g p g s/\(\&\|_\|-\|\*\)\(\&\|_\|-\|\*\)*//g p d bextendvars; }
the debug switch 'l' in first line of multiline pattern should match lines ending * matches lines , outputs
&&a_x*\n&&b_x\bx\n&&c_x*$
in faulty, combined version. when piping it, sed correctly recognizes pattern:
&&a_x*$ ... &&c_x*$
faulty output (combined version):
&&a_x*\n&&b_x\nbx\n&&c_x*$ a_x b_x bx c_x ax bx bx cx
correct ouput (piped version):
&&a_x*$ a_x ax &&b_x bx &&c_x*$ c_x cx
i run script with
sed -f [scriptname] <old >new
in version haved removed &&
&&b_x
jet.
how can sed recognize correct pattern if execute statements in 1 script? why sed fail match single line ending *
?
thanks help!
the reason code never loops again doesn't have loop condition; it's inside loop, run
d
...which aborts processing of current input line. constructed several lines in pattern space input line of no consequence; d
tells sed stop doing, read next line of input (if there one) , start on that.
anyway, approach seems overly complicated me. i'd suggest (in gnu parlance, because mechanism more obvious in gnu sed code)
#!/bin/sed -rf s/\s*(^|&&|;)\s*/\n/g # split tokens onto several lines, make sure # there's newline in front of each (so next # regex matches all) s/(\n[^\n])_x\*/\1_x\1x/g # match lines end _x*, expand # \nfoo_x\nfoox s/^\n*// # remove leading newlines (we put @ least 1 # there in beginning)
you seem have taken great pains make code work non-gnu sed, here's posix version same thing:
#!/bin/sed -f s/[[:space:]]*&&[[:space:]]*/\ /g s/[[:space:]]*;[[:space:]]*/\ /g s/^/\ / s/\(\n[^\n]\)_x\*/\1_x\1x/g s/^\ *//
this removes whitespaces around tokens. seemed sensible thing do. if don't want happen, space-matching parts have removed code, , provisions have made whitespace @ end of token line.
#!/bin/sed -rf s/^|&&|;/\n/g s/(\n[^\n])_x\*([[:blank:]]*)/\1_x\1x\2/g s/^\n//
is possible adaptation of gnu sed code.
Comments
Post a Comment