bash - Using Perl to replace a string in only a certain line of file -
i have script using large scale find , replace. when match found in particular file, record file name, , line number.
what want each file name, line number pair, change string <foo>
<bar>
on line of file.
in shell script, executing find , replace command on file given line number...
run=`perl -pi -e "s/$find/$replace/ if $. = $linenum" $file`
this believe has been ignoring $. = $linenum
, s/$find/$replace/
on whole file, bad.
any ideas how can this?
you using assignment =
instead of comparison ==
.
use:
perl -pi -e "s/$find/$replace/ if $. == $linenum" $file
where there caveats content of $find
, $replace
, $linenum
aren't going problem. caveats issues such $find
cannot contain slash; $replace
can't contain slash either; $linenum
needs line number; beware other extraneous characters confuse code.
i don't see why you'd want capture standard output of perl process when writes file, not standard output. so, assignment run
implausible. , if necessary, better off using run=$(perl …)
$()
notation in place of `…`
.
Comments
Post a Comment