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
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
perl -pi -e 'BEGIN{local $/; $r = <STDIN>} s/ktr_updater/$r/g
' file < other-file
Would edit file
i
n-place and s
ubstitude every (g
lobally) occurrence of ktr_updater
with $r
, $r
having been initialled at the BEGIN
ning with what could be read from STDIN
(in one go as the record delimiter was unset, by declaring it without a value local
ly 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
ktr_updater
occur within one line in the original file, and if so, should only that part of the line be replaced by the contents of the other file? – Kusalananda May 10 '18 at 11:01