Replacing multiple delimited blocks in sed -
i have body of text contains groups delimited <>,## or ||. blocks never overlap, may cross multiple lines, so:
#a emphasized line# line #emphasis inside#. #several lines of text emphasis# no emphasis line #with# multiple #emphasis#. line <with > |text of| #each type#. i'm attempting replace each pair of delimiters [ , ] and put final delimiter after ]; example last line should be:
line [with ]> [text of]| [each type]#. i have formed sed script first part:
sed -e ':left s/[#|<]/[/; t right; n; b left :right s/[#|>]/]/; t left;n; b right' but when try use & (or (..) + \1) put character in this:
sed -e ':left s/[#|<]/[/; t right; n; b left :right s/[#|>]/]&/; t left;n; b right' i following:
[a emphasized line][ line ][emphasis inside][. ][several lines of text emphasis][ no emphasis line ][with][ multiple ][emphasis][. line [with ]]]]]]> [text of[ [each type[. i'm not sure has gone wrong here though - appears screwing pattern block in way. replace 3 calls (hardcoded 1 per match type), seems excessive.
try following command. reads whole file in memory , global substitutions each pair of delimiters:
sed -e ' :a $! { n; ba }; s/#\([^#]*\)#/[\1]#/g; s/<\([^>]*\)>/[\1]>/g; s/|\([^|]*\)|/[\1]|/g ' infile it yields:
[a emphasized line]# line [emphasis inside]#. [several lines of text emphasis]# no emphasis line [with]# multiple [emphasis]#. line [with ]> [text of]| [each type]#.
Comments
Post a Comment