Anonymous user
Globally replace text in several files: Difference between revisions
Globally replace text in several files (view source)
Revision as of 01:16, 14 September 2014
, 9 years agoadd UNIX Shell
(add Sed) |
(add UNIX Shell) |
||
Line 1,184:
(put-string edited outfile)
(rename-path `@fname.tmp` fname))))</lang>
=={{header|UNIX Shell}}==
{{works with|bash}}
<lang bash>replace() {
local search=$1 replace=$2
local file lines line
shift 2
for file in "$@"; do
lines=()
while IFS= read -r line; do
lines+=( "${line//$search/$replace}" )
done < "$file"
printf "%s\n" "${lines[@]}" > "$file"
done
}
replace "Goodbye London!" "Hello New York!" a.txt b.txt c.txt</lang>
{{works with|ksh93}}
<lang bash>function replace {
typeset search=$1 replace=$2
typeset file lines line
shift 2
for file in "$@"; do
lines=()
while IFS= read -r line; do
lines+=( "${line//$search/$replace}" )
done < "$file"
printf "%s\n" "${lines[@]}" > "$file"
done
}
replace "Goodbye London!" "Hello New York!" a.txt b.txt c.txt</lang>
=={{header|VBScript}}==
|