regex - sed backreferences returning their numerical index rather than their value -
weird problem here don't seem see repeated anywhere else, posting here. in advance.
i have following multiline sed code printing further sed , copy commands script (yep, using script insert code script). code looks this:
sed -i -r '/(rpub )([$][a-za-z0-9])/i\ sed -i '\''/#pbs -n/d'\'' \1\ cp \1 '"$filevariable"'' $masterscript
which supposed following:
1.) open master script
2.) navigate each instance of rpub $[a-za-z0-9] in script
3.) insert second line (sed) , third line (cp) lines before rpub instance, using \1 backreference of matched $[a-za-z0-9] step 1.
this works great; lines print enough in relation each other. however, of \1 references appearing explicitly, minus backslashes. of \1's appearing 1.
i know pattern match specifications working correctly, nail instances of rpub $[a-za-z0-9] enough, guess i'm not understanding use of backreferences. see going on here?
thanks.
edit 1
special ed morton below, implemented following, gets me 99% closer, still can't close gap unexpected behavior:
awk -v fv="$filevariable" ' match($0, /rpub( [$][[:alnum:]])/, a) { print "sed -i '\''/#pbs -n/d'\''", a[1] } 1' "$masterscript" > tmpfile && mv tmpfile "$masterscript"
note: removed 1 of multiline print statements, isn't important here. but, said, though gets me closer still having issue printed lines appear between every line in masterscript; if matching function considering every line match. fault, should have specified i'd following occur:
stuff here stuff here rpub $name stuff here rpub $othername stuff here
would become:
stuff here stuff here inserted line $name rpub $name stuff here insertedline $othername rpub $othername
any appreciated. thanks!
it looks you're trying written in awk as:
awk -i inplace -v fv="$filevariable" ' match($0,/rpub ([$][[:alnum:]])/,a) { print "sed -i \"/#pbs -n/d\", a[1] print "cp", a[1], fv } 1' "$masterscript"
but without sample input , expected output it's guess.
the above uses gnu awk inplace editing , 3rd arg match().
Comments
Post a Comment