0

I want to search for characters in file and replace with file containing contents: search for ktr_updater in file and replace with file contents from another file using Unix commands.

sed -i 's/ktr_updater/'"$ktrupdate"'/g' samplejob.kjb
αғsнιη
  • 41,407
jerin
  • 19

1 Answers1

1
perl -pi -e 'BEGIN{local $/; $r = <STDIN>} s/ktr_updater/$r/g
            ' file < other-file

Would edit file in-place and substitude every (globally) occurrence of ktr_updater with $r, $r having been initialled at the BEGINning with what could be read from STDIN (in one go as the record delimiter was unset, by declaring it without a value locally to the BEGIN statement), stdin having been redirected to the other-file.

With GNU sed or compatible, you could do the same by storing the content of the file in a shell variable, convert it to something that is suitable as a sed s command replacement and call sed with that:

repl=$(cat other-file; echo .); repl=${repl%.}
escaped_repl=$(printf '%s\n' "$repl" | sed 's:[\/&]:\\&:g;$!s/$/\\/')
sed -i "s/ktr_updater/$escaped_repl/g" file