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

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -