Jump to content

Globally replace text in several files: Difference between revisions

add 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}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.