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

Card Puncher Data Processing

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:

  • searchTermRegularExpression is the search term
  • the option -i turns the search to be case insensitive

Regular expression

And

  • Sample File
Same Line                                                       Same Line
                                                              > New Line
Same Line 2                                                     Same Line 2
Different Line                                                | Different Line :)

  • Code
cat diff.txt | grep '[\>\|]'
  • Output:
> 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

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

with Linux - find command

Grep process without the Grep process

  • Standard Ps with Grep:
$ 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
  • Grep without Grep
$ ps -ef | grep nqsserver | grep -v grep
oracle   23915 20779  0 Mar05 ?        00:06:28 nqsserver -quiet
  • Grep without Grep shortcut (for grep -v)
$ 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

  • Don't show line with jar
cat myFile | grep -v 'jar'
  • With the L option: All ini files *SQL*.ini that doesnot have encrypt=true
grep -r -i -L --include "*.ini" "encrypt=true"  .

Keep header

  • Capture the first line with read in the variable a
  • Print it
  • and then grep the rest
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





Discover More
CPU - Linux

CPU in a Linux context You can use the top or htop command line to get CPU monitoring metrics. where: x86_64 means: a X86 architecture with a 64 bit word size 1 block of information...
Card Puncher Data Processing
DOS - Findstr (The grep) command

Findstr is an utility that emulates grep.
Card Puncher Data Processing
How to replace in bulk a text in multiple file with a bash pipeline

An step by step that shows you how to create bash pipeline to replace in bulk text in files
Bash Liste Des Attaques Ovh
Linux - File

Linux file management See Using Parameters Expansion Removal From a path string where the file does not exist dirname returns the first parent of an existing path file. ...
Linux Package Installation
Linux - RPM package (RPM: Red Hat Package Manager)

RPM are packages file that you can install on Linux system. This package format was created by Red Hat and standardized by the LSB. It's widely adopted, including in openSUSE. They are .cpio files...
Linux - User (Uid)

Every user who has access to a Linux system needs a login and a password. The numeric identifier are: The user identifier: called a UID (user ID). The group identifier: called the GID (group...
Card Puncher Data Processing
Oracle - SQL_TRACE

Set a trace identifier: The trace file identifier is added to the name of the trace files. Set Trace on: Perform SQL Actions Set off Where are the trace files? To find the trace file on...
Bash Liste Des Attaques Ovh
Sh - Hidden files

Files which the name start with a dot, are hidden. You can see them with the “-a” option of the ls command.
Card Puncher Data Processing
Shell Data Processing - Awk (grep and sed) - Output filtering

The awk command is a filter that implements a language that is dedicated to text processing and combines the functions of: grep and sed AWK is a (tool|language) for event-based data processing....
Card Puncher Data Processing
Shell Data Processing - Filter (Stream Operator)

This page is pipeline operator in a shell language. They are known as filter in a shell language. It is a computer program or shell command (subroutine) that: read from standard input (stream)...



Share this page:
Follow us:
Task Runner