Thursday 2 February 2012

grep in linux


* Killing all firefox processess in one line
#  ps xa| grep firefox| grep -v grep | awk '{print $1}' | xargs kill -9

grep -v grep ------ will avoid the pid of grep ( --invert-match )

* To select only the uncommented lines from large configuration file use -E / -v

-E ----  for specifying regular expression
-v ----   for inverted selection

# grep -vE '^#|^$' /etc/squid/squid.conf

* Grep a particular string from a file
# grep "this" filename

* Checking for a given string in multiple file 
For that copy the file ctechz_file to ctechz_file1. The grep output will also include the file name in front of the line that matched the specific pattern.

# cp ctechz_file ctechz_file1

# grep "this" demo_*

* Case insensitive search using grep -i
# grep -i "string" file

it will search CTECHZ and ctechz, case insensitively.

* Pattern Search
# cat filename | grep 'pattern'
# command | grep 'pattern/data'
# grep --color 'patern' fileName 
* grep recursive -- checking all files under var for the pattern 
# grep -r "pattern" /var/
* Checking full words, not for sub-strings use grep -w
# grep -iw "is" demo_file
* search two different pattern using egrep
# egrep -w 'word1|word2' /path/to/file
* showing N lines after the pattern match
# grep -A 3 -i "pattern" filename
* shows line when words has been matched
# grep -c 'pattern' filename
* shows the invert match
# grep -v 'pattern' filename
 it matches only lines that do not contain the given word
* Displaying line numbers when showing output using grep -n
#  grep -n "pattern" filename
 
Search the pattern in all files under a directory
# grep -ril "pattern" /directory
 

No comments:

Post a Comment