Wednesday, 28 October 2015

linux  grep  command with logical OR/AND operation


->OR  Operation
grep -nrl --color 'TOM \| DICK \|  HARRY' --include="*.txt"  *
grep -nrl --color 'sav2_base_test\|run_phase\|main_phase' --include="*.txt" * | wc -l                                    
This command lists TXT file names containing any of these keywords TOM , DICK or HARRY anywhere in file.

->AND Operation
grep -l  TOM * --include="*.txt" | xargs grep -l DICK | xargs grep -l HARRY 
grep -l  TOM * --include="*.txt" | xargs grep -l DICK | xargs grep -l HARRY | wc -l

This command lists TXT file names containing all of these keywords TOM , DICK or HARRY anywhere in file.


-->Find particular keyword and replace by other keyword in all files of given directory
find . -type f -exec sed -i 's/TOM/TOMMY/g' {} +
 
This command replaces all keywords TOM by TOMMY in all files of current directory.
For more info visit.
http://unix.stackexchange.com/questions/112023/how-can-i-replace-a-string-in-a-files 

No comments:

Post a Comment