Today I found myself in a situation where I had to do the same global substitution for every file in a directory. I considered writing a script to handle it, but I wanted to try using a Perl one-liner to get the job done.
Given a directory containing a file with the following contents:
This is a XML fileI can substitute every occurence of XML with text using:
This is a XML file
$ perl -i -pe 's/XML/text/g' file1.xml
This is a text fileIf I wanted to do the substitution for every file in the current directory, I can wrap the command in a bash for loop:
This is a text file
$ for i in `ls`; do perl -i -pe 's/XML/text/g' $i; done
This isn't revolutionary by any means, but its harder for me to forget this technique if I post it here.
You can also do a similar thing inside a file using vi.
ReplyDelete:%s/oldtext/newtext/g