I needed to change some text inside a file. Actually multiple files and I did not know an easy way to do it. I searched online and I came accross a little simple one liner.
This is based in this question that was posted on a message board.
##################################################
I am trying to do a search on an entire directory, finding all the instances of the text “applicationX” and replacing all instances with the text “applicationY”.
I created the following to find all instances and print the files that contain the search text. But now I don’t know how to replace the text.
find . -type f -exec grep “applicationX” {} \; -print
Please help..thanks
##########################################
An here is the reply where I found the one liner.
###########################################
or you could just aswell use PERLs inplace replacement (http://www.spiration.co.uk/post/522) as in the following one-liner:
find /path/to/start/from/ -type f | xargs perl -pi -e ’s/applicationX/applicationY/g’
christo
############################
This is based on this post .
To replace all occurences of the word “hello” with the word “harrow” in files in the current directory, use perl’s in-place substitusion from the command line as follows:
perl -pi -e ’s/hello/harrow/g’ *.txt
note, the ‘g’ at the end makes it match [gredily|globally] - ie it will to multiple matches in a single line. You can add an ‘i’ for case insensitivity.
Full regular expressions can be used, and the *.txt can of course be *.html, *.anything, or just *
christo
——————————
###############################################
I used that one line and changed to match my settings and I created a simple txt file with the Words March and I wanted to change it to April
I changed the one line to
find -type f | xargs perl -pi -e ’s/March/April/g’
and I ran that one liner inside the folder where I had my text file and it changed all the instances of March to April.
:)
I am keeping this tip on this blog to remind myself if I even need to do this again.