Globally replace text in several files: Difference between revisions

jq
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}}==
2,458

edits