Table of Contents

Shell Data Processing - Global Regular Expression Print (GREP command) (line filtering, word search)

About

grep stands for “global regular expression print”.

Grep searches lines of a file that match a regular expression pattern and returns them.

It is by default case sensitive.

Output

Only Filename

grep -Rl term /directory

searches all files in a directory and outputs only filenames with matched results.

Example

Basic

grep -i searchTermRegularExpression my/Path/File/RegularExpressionNameFile

where:

Regular expression

And

Same Line                                                       Same Line
                                                              > New Line
Same Line 2                                                     Same Line 2
Different Line                                                | Different Line :)

cat diff.txt | grep '[\>\|]'
> New Line
Different Line                                                | Different Line :)

Or

See also egrep. Example: select the line that have user = ou group =

cat /etc/php-fpm.d/www.conf  | egrep 'user =|group ='
user = www-data
group = www-data

Search within a directory

grep -R SearchPattern  /myPath/ToADir
find . -type f -exec grep -l "word"

with Linux - find command

Grep process without the Grep process

$ ps -ef | grep nqsserver
oracle    4087  4018  0 13:19 pts/0    00:00:00 grep nqsserver
oracle   23915 20779  0 Mar05 ?        00:06:28 nqsserver -quiet
$ ps -ef | grep nqsserver | grep -v grep
oracle   23915 20779  0 Mar05 ?        00:06:28 nqsserver -quiet
$ ps -ef | grep [n]qsserver
oracle   23915 20779  0 Mar05 ?        00:06:28 nqsserver -quiet

Grep hidden files

Sh - Hidden files

grep pattern .*

Example

grep LD_LIBRARY_PATH .*
.bash_history:echo $LD_LIBRARY_PATH
.bash_profile:  export LD_LIBRARY_PATH=${TT_HOME}/lib:${TT_HOME}/ttoracle_home/instantclient_11_2:${LD_LIBRARY_PATH}

Or

grep -iE 'first|second'

Not

cat myFile | grep -v 'jar'
grep -r -i -L --include "*.ini" "encrypt=true"  .

Keep header

ls | { IFS= read line1; echo $line1; grep fileName }

If the header has two lines

command | { IFS= read line1; read line2; echo $line1; echo $line2; grep pattern }

Documentation / Reference