Wednesday 30 October 2013

Grep and replace linux

Finding and replacing using grep in linux / centos 6+ 

1. The syntax is 
 grep -rl 'findWord' path_to_your_file_name | xargs sed -i 's/findWord/replaceWord/g'

2. Searching for all files in the current directory for the term windows and replace it with linux
grep -rl 'windows' ./ | xargs sed -i 's/windows/linux/g'
s/ - indicates the start
/g - indicates the end
./ - the current directory

3. You can use regex also - eg.
grep -rl '{:\".*\",\"hello\":\"' data.txt | xargs sed -i 's/{:\".*\",\"hello\":\"//g'