regex - Replace text between 2 strings (inclusive) -
is there perl one-liner replace text between 2 strings (inclusive)? illustration in text below need replace between 'rrpv_bits = 2' , 'options.cacheline_size' 'num_sets = 512' everywhere in text these 2 tag strings found. original text desired one.
original:
repl = replacementpolicy(rrpv_bits = 2, ins = 2, num_sets = l2_cache.size.getvalue() / options.l2_assoc / options.cacheline_size, assoc = options.l2_assoc) desired:
repl = replacementpolicy(num_sets = 512, assoc = options.l2_assoc) if perl one-liner can't there else (linux, one-liner) can?
thank you
if exist together, perl one-liner:
perl -0777 -pe 's/rrpv_bits = 2.*?options\.cacheline_size/num_sets = 512/gs' file.txt > newfile.txt however, approach greedy. happen if first boundary matches not sec in replacepolicy call? regex eat lines until finds sec replacepolicy contain end condition.
therefore, protect against this, can limit characters between boundary conditions allow balanced parenthesis. lock matching within parameters of replacepolicy:
perl -0777 -pe 's/rrpv_bits = 2((?:[^()]*|\((?1)\))*)options\.cacheline_size/num_sets = 512/gs;' file.txt > newfile.txt explanation: switches:
-0777: slurp entire file -p: creates while(<>){...; print} loop each “line” in input file. -e: tells perl execute code on command line. regex perl
No comments:
Post a Comment