Search and replace a word in multiple files from command line in linux, mac

Search and replace a word in multiple files from command line in linux, mac

For search and replace words in many files using command line and using ack:

Open Terminal Console :

For search only:

ack -a 'border-radiused'

For search and list only the files where contain words:

ack -a -l 'border-radiused'

Y now we search the word border-radiused and we will replace instead borderradiused in the current directory in all the files where contain that word with next command:

ack -a -l 'border-radiused'| xargs sed -i '' -e 's/border-radiused/borderradiused/g'

After to run the before command if we search again the word 

ack -a 'border-radiused'

Using silver searcher

ag "stub_paperclip_processing\(CuratedBook\)" --nogroup | awk '{print substr($1,1,index($1,":")-1);}' | xargs sed -i '' -e "s/CuratedBook/BookImage/"

Listing the files that contain a specific string

ag "stub_paperclip_processing\(CuratedGame\)" --nogroup | awk '{print substr($1,1,index($1,":")-1);}'

We can see that any file contain the word

And that’s it, see you

No Comments

Post A Comment