Sunday, March 31, 2013

Useful Linux search / replace commands



Search for a text string against all *.log files of current folder:

1. List files of given type w/given string:

find . -name "*.log" -exec grep -l "<search_text>" {} \;

2. Print all matches in files of given type:

find . -name "*.log" -exec grep -A 3 -B 3 "<search_text>" {} \;

where -A and -B indicates the number of lines to print after and before found matches.

3. Replace strings in files:

replace 'windows' --> 'linux' in all files of current dir

grep -rl 'windows' . | xargs sed -i 's/windows/linux/g'

And, sure, you can use the UI tools for simple search/replace actions.
Example: regexxer search tool.

No comments:

Post a Comment