Shell command to replace multiline regex in place

With a single line regex, you can use -p

and $_.sub!

:

$ cat file.txt
<a
a>
c
$ ruby -i -pe '$_.sub!("a", "b")' file.txt
$ cat file.txt
<b
b>
c

      

Is there a short way to replace multi-line patterns? I am currently using something like this:

$ ruby -i -e 'print *readlines.join.sub(/<.*>/m, "d")' file.txt
$ cat file.txt
d
c

      

+3


source to share


1 answer


Using it gets(nil)

saves you as much as 6 characters :)

ruby -i -e 'print gets(nil).sub(/<.*>/m, "d")' file.txt

      



From the gets

docs :

An optional argument specifies the record separator. A separator is included in the content of each entry. The nil delimiter reads all content. [...]

+2


source







All Articles