Showing posts with label grep. Show all posts
Showing posts with label grep. Show all posts

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 

Wednesday, 22 April 2015

Linux Grep Command


grep "literal_string" filename
 
grep "string" FILE_PATTERN
 
grep -i "string" FILE
 
grep "REGEX" filename
 
grep -iw "is" demo_file
 
grep -A <N> "string" FILENAME
 
grep -B <N> "string" FILENAME
 
grep -C 2 "Example" demo_text 

export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'
grep this demo_file
 
grep -r "DJ" * 

grep -v "go" demo_text
 
grep -v -e "pattern" -e "pattern"
 
grep -c "pattern" filename
 
grep -v -c this demo_file
 
grep -l this demo_*
 
grep -o "is.*line" demo_file
 
grep -o -b "pattern" file
 
grep -n "go" demo_text