Globally replace text in several files: Difference between revisions
Globally replace text in several files (view source)
Revision as of 06:27, 17 July 2017
, 6 years agojq
m (→{{header|Sidef}}: updated code) |
(jq) |
||
Line 679:
}
}</lang>
=={{header|jq}}==
{{works with|jq|1.5}}
jq delegates filename manipulation to the shell. For simplicity, in the following we assume the availability of `sponge` to simplify the mechanics of editing a file "in-place".
<lang bash>for file
do
jq -Rr 'gsub($from; $to)' --arg from 'Goodbye London!' --arg to 'Hello New York!' "$file" |
sponge "$file"
done</lang>
The jq filter used above is `gsub/2`, which however is designed for regular expressions. Here is a string-oriented alternative:
<lang jq>def gsubst($from; $to):
($from | length) as $len
| def g:
index($from) as $ix
| if $ix then .[:$ix] + $to + (.[($ix+$len):] | g) else . end;
g;</lang>
=={{header|Julia}}==
Line 689 ⟶ 706:
end
end</lang>
=={{header|Lasso}}==
|