Skip to content

c4arl0s/grep

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 

Repository files navigation

grep

File pattern searcher

SYNOPSIS
     grep [-abcdDEFGHhIiJLlMmnOopqRSsUVvwXxZz] [-A num] [-B num] [-C num] [-e pattern] [-f file] [--binary-files=value] [--color[=when]]
          [--colour[=when]] [--context=num] [--label] [--line-buffered] [--null] [pattern] [file ...]

Example

  • Find all occurrences of the pattern ‘patricia’ in a file:
grep 'patricia' myfile

grep -w

  • Same as above but looking only for complete words:
grep -w 'patricia' myfile
  • Count occurrences of the exact pattern ‘FOO’ :

grep -c

grep -c FOO myfile

greo -c -i

  • Same as above but ignoring case:
$ grep -c -i FOO myfile

grep at the beginning of a line

  • Find all occurrences of the pattern ‘.Pp’ at the beginning of a line:
grep '^\.Pp' myfile

The apostrophes ensure the entire expression is evaluated by grep instead of by the user's shell. The caret ‘^’ matches the null string at the beginning of a line, and the ‘\’ escapes the ‘.’, which would otherwise match any character.

grep -v -e

  • Find all lines in a file which do not contain the words ‘foo’ or ‘bar’:
grep -v -e 'foo' -e 'bar' myfile

egrep

  • Peruse the file ‘calendar’ looking for either 19, 20, or 25 using extended regular expressions:
egrep '19|20|25' calendar

grep -H -R

  • Show matching lines and the name of the ‘*.h’ files which contain the pattern ‘FIXME’. Do the search recursively from the /usr/src/sys/arm directory
grep -H -R FIXME --include="*.h" /usr/src/sys/arm/

grep -l -R

  • Same as above but show only the name of the matching file:
grep -l -R FIXME --include="*.h" /usr/src/sys/arm/

grep -b --colour -n

  • Show lines containing the text ‘foo’. The matching part of the output is colored and every line is prefixed with the line number and the offset in the file for those lines that matched.
grep -b --colour -n foo myfile

grep -E -f -

  • Show lines that match the extended regular expression patterns read from the standard input:
echo -e 'Free\nBSD\nAll.*reserved' | grep -E -f - myfile

grep -B3 -A1 -E

  • Show lines from the output of the pciconf(8) command matching the specified extended regular expression along with three lines of leading context and one line of trailing context:
pciconf -lv | grep -B3 -A1 -E 'class.*=.*storage'

grep -q

  • Suppress any output and use the exit status to show an appropriate message:
grep -q foo myfile && echo File matches

grep -E (OR)

-E, --extended-regexp
             Interpret pattern as an extended regular expression (i.e., force grep to behave as egrep).

Find lines that contains "small is good" or "University".

grep -E -n "small is good|University" history-of-unix.txt

option -n was added to print the line that the pattern was found.

Screenshot 2023-11-24 at 12 44 09 p m

grep -E (AND)

-E, --extended-regexp
             Interpret pattern as an extended regular expression (i.e., force grep to behave as egrep).

Find lines that contains "small is good" and "Unix".

grep -E -n "small is good.*Unix" history-of-unix.txt

option -n was added to print the line that the pattern was found.

Screenshot 2023-11-24 at 12 45 11 p m

grep -o (Prints only the matching part of the lines)

-o, --only-matching
             Prints only the matching part of the lines.

Print strings that match a pattern over a line or lines. (example TXT-1245, TXT-123456, TXT-1234567, any size of the number)

Content of file.txt

TXT-1234, hdjsahdasdh, dhjsahdashdj TXT-5635261, 78327183, TXT-5621562, TXT-53625, etc 
TXT-1234, hdjsahdasdh, dhjsahdashdj TXT-5635261, 78327183, TXT-5621562, TXT-53625, etc 

Executing command:

grep -o "TXT-[0-9]*" file.txt

Console output

TXT-1234
TXT-5635261
TXT-5621562
TXT-53625
TXT-1234
TXT-5635261
TXT-5621562
TXT-53625

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published