Idiomatic write

Hi,

I want to search for a pattern in a file, replace it with something and
then write it back to the file. In the shell-script idiom,
I’d do:

sed /pattern/replace myfile > tmpfile
mv tmpfile myfile

In Ruby, I could do the same thing, slurp the file into a string and then
write it back again. I was wondering if there’s a more idiomatic way of
doing it, in place, i.e., edit the file straight away. This could probably
be more practical when the file to be edited is a huge one…

Any pointers(sic)…

regs
Vivek

···

Hi,

Vivek Nallur nvivek@konark.ncst.ernet.in writes:

I want to search for a pattern in a file, replace it with something and
then write it back to the file. In the shell-script idiom,
I’d do:

sed /pattern/replace myfile > tmpfile
mv tmpfile myfile

In Ruby, I could do the same thing, slurp the file into a string and then
write it back again. I was wondering if there’s a more idiomatic way of
doing it, in place, i.e., edit the file straight away. This could probably
be more practical when the file to be edited is a huge one…

Any pointers(sic)…

% ruby -i~ -pe ‘gsub(/pattern/, “replace”)’ myfile

···


eban

WATANABE Hirofumi graced us by uttering:

Vivek Nallur nvivek@konark.ncst.ernet.in writes:

I want to search for a pattern in a file, replace it with something and
then write it back to the file. In the shell-script idiom,
I’d do:

sed /pattern/replace myfile > tmpfile
mv tmpfile myfile

In Ruby, I could do the same thing, slurp the file into a string and then
write it back again. I was wondering if there’s a more idiomatic way of
doing it, in place, i.e., edit the file straight away. This could probably
be more practical when the file to be edited is a huge one…

Any pointers(sic)…

% ruby -i~ -pe ‘gsub(/pattern/, “replace”)’ myfile

Does this work if /pattern/ spans lines?

Tim Hammerquist

···


We all know linux is great…it does infinite loops in 5 seconds.
– Linus Torvalds