Grep: Difference between revisions

From wiki
Jump to navigation Jump to search
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 4: Line 4:
:Find lines matching pattern in any of the files.
:Find lines matching pattern in any of the files.
:When multiple files are scanned the filename is reported too (see -h below)
:When multiple files are scanned the filename is reported too (see -h below)
;zgrep <pattern>  <files>
:Search possibly gzipped files.


=Useful options=
=Useful options=
Line 12: Line 15:
;-l
;-l
:Only report filename of files with matches
:Only report filename of files with matches
;-h
;-H -h
:Suppress reporting of filenames when multiple files are scanned.  
:Force or suppress reporting of filenames.
;-n
;-n
:Report linenumber of matches too
:Report linenumber of matches too
;-f <file>
;-f <file>
:Get patterns from <file>. One(1) pattern per line.
:Get patterns from <file>. One(1) pattern per line.
;-C 2
:Print the context meaning 2 line before and after the matching line.


=Return value ( $? )=
=Return value ( $? )=

Latest revision as of 09:55, 3 September 2024

Find pattern in data stream (files, standard input)

grep <pattern> <files>
Find lines matching pattern in any of the files.
When multiple files are scanned the filename is reported too (see -h below)
zgrep <pattern> <files>
Search possibly gzipped files.

Useful options

-i
Ignore case
-v
Negate the match, show all lines not having the pattern
-l
Only report filename of files with matches
-H -h
Force or suppress reporting of filenames.
-n
Report linenumber of matches too
-f <file>
Get patterns from <file>. One(1) pattern per line.
-C 2
Print the context meaning 2 line before and after the matching line.

Return value ( $? )

0 (true) if one or more matches are found

1 (false) if no matches are found