grep ๋ช…๋ น์–ด
Linux/Shell

grep ๋ช…๋ น์–ด

1. grep

ํŠน์ • ํŒŒ์ผ ์•ˆ์—์„œ ํŠน์ • ๋ฌธ์ž์—ด์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ๋ผ์ธ์„ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๋ช…๋ น์–ด ์ž…๋‹ˆ๋‹ค. ๋‹จ์ผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ๋ณด๋‹ค๋Š” ํŒŒ์ดํ”„๋ผ์ธ์„ ์ด์šฉํ•ด ๋‹ค๋ฅธ ๋ช…๋ น์–ด์™€ ํ•จ๊ป˜ ๋งŽ์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ps ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ํ˜„์žฌ ์‹คํ–‰์ค‘์ธ ํ”„๋กœ์„ธ์Šค ์ค‘ ํŠน์ • ๋ฌธ์ž์—ด์„ ํฌํ•จ ํ”„๋กœ์„ธ์Šค๋งŒ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

> grep [OPTION] [MESSAGE PATTERN] [FILE]
> grep --help
usage: grep [-abcDEFGHhIiJLlmnOoqRSsUVvwxZ] [-A num] [-B num] [-C[num]]
	[-e pattern] [-f file] [--binary-files=value] [--color=when]
	[--context[=num]] [--directories=action] [--label] [--line-buffered]
	[--null] [pattern] [file ...]

โš™๏ธ grep manual 

๋”๋ณด๊ธฐ

NAME
     grep, egrep, fgrep, zgrep, zegrep, zfgrep -- file pattern searcher

SYNOPSIS
     grep [-abcdDEFGHhIiJLlmnOopqRSsUVvwxZ] [-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 ...]

DESCRIPTION
     The grep utility searches any given input files, selecting lines that match one or more patterns.  By default, a pattern matches an input line if the regular expression (RE) in
     the pattern matches the input line without its trailing newline.  An empty expression matches every line.  Each input line that matches at least one of the patterns is written to
     the standard output.

     grep is used for simple patterns and basic regular expressions (BREs); egrep can handle extended regular expressions (EREs).  See re_format(7) for more information on regular
     expressions.  fgrep is quicker than both grep and egrep, but can only handle fixed patterns (i.e. it does not interpret regular expressions).  Patterns may consist of one or more
     lines, allowing any of the pattern lines to match a portion of the input.

     zgrep, zegrep, and zfgrep act like grep, egrep, and fgrep, respectively, but accept input files compressed with the compress(1) or gzip(1) compression utilities.

     The following options are available:

     -A num, --after-context=num
             Print num lines of trailing context after each match.  See also the -B and -C options.

     -a, --text
             Treat all files as ASCII text.  Normally grep will simply print ``Binary file ... matches'' if files contain binary characters.  Use of this option forces grep to output
             lines matching the specified pattern.

     -B num, --before-context=num
             Print num lines of leading context before each match.  See also the -A and -C options.

     -b, --byte-offset
             The offset in bytes of a matched pattern is displayed in front of the respective matched line.

     -C[num, --context=num]
             Print num lines of leading and trailing context surrounding each match.  The default is 2 and is equivalent to -A 2 -B 2.  Note: no whitespace may be given between the
             option and its argument.

     -c, --count
             Only a count of selected lines is written to standard output.

     --colour=[when, --color=[when]]
             Mark up the matching text with the expression stored in GREP_COLOR environment variable.  The possible values of when can be `never', `always' or `auto'.

     -D action, --devices=action
             Specify the demanded action for devices, FIFOs and sockets.  The default action is `read', which means, that they are read as if they were normal files.  If the action is
             set to `skip', devices will be silently skipped.

     -d action, --directories=action
             Specify the demanded action for directories.  It is `read' by default, which means that the directories are read in the same manner as normal files.  Other possible val-
             ues are `skip' to silently ignore the directories, and `recurse' to read them recursively, which has the same effect as the -R and -r option.

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

     -e pattern, --regexp=pattern
             Specify a pattern used during the search of the input: an input line is selected if it matches any of the specified patterns.  This option is most useful when multiple -e
             options are used to specify multiple patterns, or when a pattern begins with a dash (`-').

     --exclude
             If specified, it excludes files matching the given filename pattern from the search.  Note that --exclude patterns take priority over --include patterns, and if no
             --include pattern is specified, all files are searched that are not excluded.  Patterns are matched to the full path specified, not only to the filename component.

     --exclude-dir
             If -R is specified, it excludes directories matching the given filename pattern from the search.  Note that --exclude-dir patterns take priority over --include-dir pat-
             terns, and if no --include-dir pattern is specified, all directories are searched that are not excluded.

     -F, --fixed-strings
             Interpret pattern as a set of fixed strings (i.e. force grep to behave as fgrep).

     -f file, --file=file
             Read one or more newline separated patterns from file.  Empty pattern lines match every input line.  Newlines are not considered part of a pattern.  If file is empty,
             nothing is matched.

     -G, --basic-regexp
             Interpret pattern as a basic regular expression (i.e. force grep to behave as traditional grep).

     -H      Always print filename headers with output lines.

     -h, --no-filename
             Never print filename headers (i.e. filenames) with output lines.

     --help  Print a brief help message.

     -I      Ignore binary files.  This option is equivalent to --binary-file=without-match option.

     -i, --ignore-case
             Perform case insensitive matching.  By default, grep is case sensitive.

     --include
             If specified, only files matching the given filename pattern are searched.  Note that --exclude patterns take priority over --include patterns.  Patterns are matched to
             the full path specified, not only to the filename component.

     --include-dir
             If -R is specified, only directories matching the given filename pattern are searched.  Note that --exclude-dir patterns take priority over --include-dir patterns.

     -J, --bz2decompress
             Decompress the bzip2(1) compressed file before looking for the text.

     -L, --files-without-match
             Only the names of files not containing selected lines are written to standard output.  Pathnames are listed once per file searched.  If the standard input is searched,
             the string ``(standard input)'' is written.

     -l, --files-with-matches
             Only the names of files containing selected lines are written to standard output.  grep will only search a file until a match has been found, making searches potentially
             less expensive.  Pathnames are listed once per file searched.  If the standard input is searched, the string ``(standard input)'' is written.

     --mmap  Use mmap(2) instead of read(2) to read input, which can result in better performance under some circumstances but can cause undefined behaviour.

     -m num, --max-count=num
             Stop reading the file after num matches.

     -n, --line-number
             Each output line is preceded by its relative line number in the file, starting at line 1.  The line number counter is reset for each file processed.  This option is
             ignored if -c, -L, -l, or -q is specified.

     --null  Prints a zero-byte after the file name.

     -O      If -R is specified, follow symbolic links only if they were explicitly listed on the command line.  The default is not to follow symbolic links.

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

     -p      If -R is specified, no symbolic links are followed.  This is the default.

     -q, --quiet, --silent
             Quiet mode: suppress normal output.  grep will only search a file until a match has been found, making searches potentially less expensive.

     -R, -r, --recursive
             Recursively search subdirectories listed.

     -S      If -R is specified, all symbolic links are followed.  The default is not to follow symbolic links.

     -s, --no-messages
             Silent mode.  Nonexistent and unreadable files are ignored (i.e. their error messages are suppressed).

     -U, --binary
             Search binary files, but do not attempt to print them.

     -V, --version
             Display version information and exit.

     -v, --invert-match
             Selected lines are those not matching any of the specified patterns.

     -w, --word-regexp
             The expression is searched for as a word (as if surrounded by `[[:<:]]' and `[[:>:]]'; see re_format(7)).

     -x, --line-regexp
             Only input lines selected against an entire fixed string or regular expression are considered to be matching lines.

     -y      Equivalent to -i.  Obsoleted.

     -Z, -z, --decompress
             Force grep to behave as zgrep.

     --binary-files=value
             Controls searching and printing of binary files.  Options are binary, the default: search binary files but do not print them; without-match: do not search binary files;
             and text: treat all files as text.

     --context[=num]
             Print num lines of leading and trailing context.  The default is 2.

     --line-buffered
             Force output to be line buffered.  By default, output is line buffered when standard output is a terminal and block buffered otherwise.

     If no file arguments are specified, the standard input is used.

ENVIRONMENT
     GREP_OPTIONS  May be used to specify default options that will be placed at the beginning of the argument list.  Backslash-escaping is not supported, unlike the behavior in GNU
                   grep.

EXIT STATUS
     The grep utility exits with one of the following values:

     0     One or more lines were selected.
     1     No lines were selected.
     >1    An error occurred.

EXAMPLES
     To find all occurrences of the word `patricia' in a file:

           $ grep 'patricia' myfile

     To 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.

     To find all lines in a file which do not contain the words `foo' or `bar':

           $ grep -v -e 'foo' -e 'bar' myfile

     A simple example of an extended regular expression:

           $ egrep '19|20|25' calendar

     Peruses the file `calendar' looking for either 19, 20, or 25.

SEE ALSO
     ed(1), ex(1), gzip(1), sed(1), re_format(7)

STANDARDS
     The grep utility is compliant with the IEEE Std 1003.1-2008 (``POSIX.1'') specification.

     The flags [-AaBbCDdGHhIJLmoPRSUVwZ] are extensions to that specification, and the behaviour of the -f flag when used with an empty pattern file is left undefined.

     All long options are provided for compatibility with GNU versions of this utility.

     Historic versions of the grep utility also supported the flags [-ruy].  This implementation supports those options; however, their use is strongly discouraged.

HISTORY
     The grep command first appeared in Version 6 AT&T UNIX.

BUGS
     The grep utility does not normalize Unicode input, so a pattern containing composed characters will not match decomposed input, and vice versa.

 

2. OPTION

> cat A.txt
"Grant me the serenity to accept the things I can not change,
the courage to change the things I can,
and wisdom to know the difference"

-Karl Paul Reinhold Niebuhr-
> cat B.txt
"Never regret.
If it's good, it's wonderful.
If it's bad, it's experience."

-Victoria Holt-

A ์˜ต์…˜ : ํŒจํ„ด๊ณผ ์ผ์น˜๋œ ๋ผ์ธ์˜ ์•„๋ž˜๋กœ N๋ฒˆ ๋ผ์ธ๊นŒ์ง€ ์ถ”๊ฐ€ ์ถœ๋ ฅ

> grep -A2 Grant A.txt
"Grant me the serenity to accept the things I can not change,
the courage to change the things I can,
and wisdom to know the difference"

B ์˜ต์…˜ : ํŒจํ„ด๊ณผ ์ผ์น˜๋œ ๋ผ์ธ์˜ ์œ„๋กœ N๋ฒˆ ๋ผ์ธ๊นŒ์ง€ ์ถ”๊ฐ€ ์ถœ๋ ฅ

> grep -B3 Karl A.txt
the courage to change the things I can,
and wisdom to know the difference"

-Karl Paul Reinhold Niebuhr-

b ์˜ต์…˜ : ์ถœ๋ ฅ ๋ผ์ธ ์•ž ๋ผ์ธ ๋ฐ”์ดํŠธ ์˜ต์…‹ ํฌํ•จํ•˜์—ฌ ์ถœ๋ ฅ

> grep -b a A.txt
0:"Grant me the serenity to accept the things I can not change,
62:the courage to change the things I can,
102:and wisdom to know the difference"
138:-Karl Paul Reinhold Niebuhr-

C ์˜ต์…˜ : ํŒจํ„ด๊ณผ ์ผ์น˜๋œ ๋ผ์ธ์˜ ์œ„/์•„๋ž˜๋กœ N๋ฒˆ ๋ผ์ธ๊นŒ์ง€ ์ถ”๊ฐ€ ์ถœ๋ ฅ

> grep -C1 courage A.txt
"Grant me the serenity to accept the things I can not change,
the courage to change the things I can,
and wisdom to know the difference"

c ์˜ต์…˜ : ํŒจํ„ด์ผ ์ผ์น˜ํ•˜๋Š” ๋ผ์ธ ์ˆ˜ ์ถœ๋ ฅ

> grep -c a A.txt
4

e ์˜ต์…˜ : ํŒจํ„ด์„ ์‚ฌ์šฉ(๊ธฐ๋ณธ๊ฐ’)

> grep -e 'th*' A.txt
"Grant me the serenity to accept the things I can not change,
the courage to change the things I can,
and wisdom to know the difference"
> grep 'th*' A.txt
"Grant me the serenity to accept the things I can not change,
the courage to change the things I can,
and wisdom to know the difference"

f ์˜ต์…˜ : ํŒŒ์ผ์„ ํŒจํ„ด์œผ๋กœ ์‚ฌ์šฉ

> cat pattern.txt
th*
> grep -f pattern.txt A.txt
"Grant me the serenity to accept the things I can not change,
the courage to change the things I can,
and wisdom to know the difference"

H ์˜ต์…˜ : ์ถœ๋ ฅ ๋ผ์ธ ์•ž ํŒŒ์ผ ์ด๋ฆ„ ํฌํ•จํ•˜์—ฌ ์ถœ๋ ฅ(๋‹ค์ค‘ ํŒŒ์ผ์—์„œ ์‹คํ–‰ ์‹œ ๊ธฐ๋ณธ์œผ๋กœ ํŒŒ์ผ ์ด๋ฆ„ ํฌํ•จํ•˜์—ฌ ์ถœ๋ ฅ)

> grep -H a A.txt
A.txt:"Grant me the serenity to accept the things I can not change,
A.txt:the courage to change the things I can,
A.txt:and wisdom to know the difference"
A.txt:-Karl Paul Reinhold Niebuhr-

h ์˜ต์…˜ : ์ถœ๋ ฅ ๋ผ์ธ ์•ž ํŒŒ์ผ ์ด๋ฆ„ ์ œ์™ธํ•˜๊ณ  ์ถœ๋ ฅ

> grep -h a A.txt B.txt
"Grant me the serenity to accept the things I can not change,
the courage to change the things I can,
and wisdom to know the difference"
-Karl Paul Reinhold Niebuhr-
If it's bad, it's experience."
-Victoria Holt-

i ์˜ต์…˜ : ๋Œ€/์†Œ๋ฌธ์ž ๋น„๊ตํ•˜์—ฌ ํŒจํ„ด ํ™•์ธ ํ›„ ์ถœ๋ ฅ

> grep grant A.txt
> grep -i grant A.txt
"Grant me the serenity to accept the things I can not change,

L ์˜ต์…˜ : ํŒจํ„ด์ด ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ํŒŒ์ผ ์ด๋ฆ„ ์ถœ๋ ฅ

> grep -L Grant *.txt
B.txt

l ์˜ต์…˜ : ํŒจํ„ด์ด ์ผ์น˜ํ•˜๋Š” ํŒŒ์ผ ์ด๋ฆ„ ์ถœ๋ ฅ

> grep -l Grant *
A.txt

m ์˜ต์…˜ : ์ตœ๋Œ€ ์ถœ๋ ฅ ๋ผ์ธ์„ ์ œํ•œํ•˜์—ฌ ์ถœ๋ ฅ

> grep -m2 a A.txt
"Grant me the serenity to accept the things I can not change,
the courage to change the things I can,

n ์˜ต์…˜ : ๋ผ์ธ ๋„˜๋ฒ„์™€ ํ•จ๊ป˜ ์ถœ๋ ฅ

> grep -n a A.txt
1:"Grant me the serenity to accept the things I can not change,
2:the courage to change the things I can,
3:and wisdom to know the difference"
5:-Karl Paul Reinhold Niebuhr-

o ์˜ต์…˜ : ๋งค์นญ๋œ ๋‹จ์–ด๋งŒ ์ถœ๋ ฅ

> grep -o Grant A.txt
Grant

q ์˜ต์…˜ : ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜์ง€ ์•Š์Œ

> grep -q a A.txt

s ์˜ต์…˜ :์—๋Ÿฌ ๋ฉ”์„ธ์ง€๋ฅผ ์ถœ๋ ฅํ•˜์ง€ ์•Š์Œ

> grep -s courage *
A.txt:the courage to change the things I can,
> grep courage *
A.txt:the courage to change the things I can,
grep: Workspace: Is a directory

v ์˜ต์…˜ : ํŒจํ„ด์ด ์ผ์น˜๋˜์ง€ ์•Š๋Š” ๋ผ์ธ๋งŒ ์ถœ๋ ฅ

> grep -v I A.txt
and wisdom to know the difference"

-Karl Paul Reinhold Niebuhr-

w ์˜ต์…˜ : ๋‹จ์–ด ๋‹จ์œ„๋กœ ํŒจํ„ด์ด ์ผ์น˜๋˜๋Š”์ง€ ํ™•์ธ ํ›„ ์ถœ๋ ฅ

> grep no A.txt
"Grant me the serenity to accept the things I can not change,
and wisdom to know the difference"
> grep -w no A.txt
> grep -w not A.txt
"Grant me the serenity to accept the things I can not change,

๊ทธ ์™ธ

๋”๋ณด๊ธฐ

a ์˜ต์…˜ : ๋ฐ”์ด๋„ˆ๋ฆฌ ํŒŒ์ผ์„ ํ…์ŠคํŠธ ํŒŒ์ผ๋กœ ์ฒ˜๋ฆฌ

d ์˜ต์…˜ : ๋””๋ ‰ํ† ๋ฆฌ ์ฒ˜๋ฆฌ ๋ฐฉ์‹ ์ง€์ •(read, recure, skip)

D ์˜ต์…˜ : ์žฅ์น˜ ํŒŒ์ผ ์ฒ˜๋ฆฌ ๋ฐฉ์‹ ์ง€์ •(read, skip)

E ์˜ต์…˜ : MESSAGE PATTERN์„ ํ™•์žฅ ์ •๊ทœ ํ‘œํ˜„์‹์œผ๋กœ ํ•ด์„

F ์˜ต์…˜ : MESSAGE PATTERN์„ ์ผ๋ฐ˜ ๋ฌธ์ž์—ด๋กœ ํ•ด์„

G ์˜ต์…˜ : MESSAGE PATTERN์„ ๊ธฐ๋ณธ ์ •๊ทœ ํ‘œํ˜„์‹์œผ๋กœ ํ•ด์„(๊ธฐ๋ณธ๊ฐ’)

P ์˜ต์…˜ : MESSAGE PATTERN์„ ํŽ„ ์ •๊ทœ ํ‘œํ˜„์‹์œผ๋กœ ํ•ด์„

I ์˜ต์…˜ : ๋ฐ”์ด๋„ˆ๋ฆฌ ํŒŒ์ผ์€ ํ™•์ธํ•˜์ง€ ์•Š์Œ

J ์˜ต์…˜ : ํŒŒ์ผ์„ ํƒ์ƒ‰ํ•˜๊ธฐ ์ „ bzip2 ์••์ถ• ํ•ด์ฒด

O ์˜ต์…˜ : ๋ช…์‹œ๋œ ์‹ฌ๋ณผ๋ฆญ ๋งํฌ๋ฅผ ๋”ฐ๋ผ๊ฐ€๋ฉฐ ์„œ๋ธŒ ๋ฐ๋ ‰ํ† ๋ฆฌ๊นŒ์ง€ ํƒ์ƒ‰

r/R ์˜ต์…˜ : ์„œ๋ธŒ ๋””๋ ‰ํ† ๋ฆฌ๊นŒ์ง€ ํƒ์ƒ‰

S ์˜ต์…˜ : ๋ชจ๋“  ์‹ฌ๋ณผ๋ฆญ ๋งํฌ๋ฅผ ๋”ฐ๋ผ๊ฐ€๋ฉฐ ์„œ๋ธŒ ๋””๋ ‰ํ† ๋ฆฌ๊นŒ์ง€ ํƒ์ƒ‰

U ์˜ต์…˜ : ๋ฐ”์ด๋„ˆ๋ฆฌ ํŒŒ์ผ๋กœ ์ทจ๊ธ‰

V ์˜ต์…˜ : grep ๋ฒ„์ „ ์ถœ๋ ฅ

x ์˜ต์…˜ : ๋ผ์ธ ์ „์ฒด์™€ ๋งค์นญ๋˜๋Š”์ง€ ํ™•์ธ

y ์˜ต์…˜ : i ์˜ต์…˜๊ณผ ๋™์ผ, ๊ตฌ์‹

z/Z ์˜ต์…˜ : zgrep์ฒ˜๋Ÿผ ๋™์ž‘

 

3. MESSAGE PATTERN

> cat test.txt
The
the
Thing
thing
Love
love
Loving
loving
It is the apple
abababc

๋ผ์ธ์ด "t"๋กœ ์‹œ์ž‘

> grep "^t" test.txt
the
thing

๋ผ์ธ์ด "e"๋กœ ๋

> grep "e$" test.txt
The
the
Love
love
It is the apple

"lo"๋กœ ์‹œ์ž‘ํ•œ 4๊ฐœ์˜ ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด ํฌํ•จ

> grep "lo.." test.txt
love
loving

"l"๋กœ ์‹œ์ž‘ํ•˜๊ณ  "e"๋กœ ๋๋‚˜๋Š” 4๊ฐœ์˜ ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด ํฌํ•จ

> grep "l..e" test.txt
love

4๊ฐœ์˜ ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด ํฌํ•จ

> grep -w "...." test.txt
Love
love

"l"๋กœ ์‹œ์ž‘ํ•˜๊ณ  "g"๋กœ ๋๋‚˜๋Š” ๋ฌธ์ž์—ด ํฌํ•จ

> grep "l.*g" test.txt
loving

"th"๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ฌธ์ž์—ด์„ ํฌํ•จ

> grep "th*" test.txt
the
thing
It is the apple

"Love" ํ˜น์€ "love" ๋ฌธ์ž์—ด์„ ํฌํ•จ

> grep "[Ll]ove" test.txt
Love
love

"A" ์ด์™ธ๋กœ ์‹œ์ž‘ํ•˜๊ณ  "ove"๋กœ ๋๋‚˜๋Š” 4๊ฐœ์˜ ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด ํฌํ•จ

> grep "[^A]ove" test.txt
Love
love

๋ผ์ธ์ด "a"๋ถ€ํ„ฐ "t"๊นŒ์ง€์˜ ๋ฌธ์ž๋กœ ์‹œ์ž‘

> grep "^[a-t]" test.txt
the
thing
love
loving
abababc

๋‹จ์–ด ๋‹จ์œ„๋กœ "lov"๋กœ ์‹œ์ž‘

> grep "\<lov" test.txt
love
loving

๋‹จ์–ด ๋‹จ์œ„๋กœ "ing"๋กœ ๋

> grep "ing\>" test.txt
Thing
thing
Loving
loving

"ab"๋ฅผ ํƒœ๊ทธ๋กœ ์‚ฌ์šฉํ•˜๋ฉฐ 1๋กœ ๋“ฑ๋ก๋˜์–ด 3๋ฒˆ ๋ฐ˜๋ณต๋œ ๋ฌธ์ž์—ด ํฌํ•จ

> grep "\(ab\)\1\1c" test.txt
abababc

"ab"๋ฅผ ํƒœ๊ทธ๋กœ ์‚ฌ์šฉํ•˜๋ฉฐ ์ตœ์†Œ 1๋ฒˆ ์ตœ๋Œ€ 3๋ฒˆ ๋ฐ˜๋ณต๋œ ๋ฌธ์ž์—ด ํฌํ•จ

> grep "ab\{1,3\}" test.txt
abababc

"the"์™€ "apple"์„ AND ์กฐ๊ฑด์œผ๋กœ ์‹คํ–‰

> grep -E "the.*apple" test.txt
It is the apple
> grep -E "the.*apple|apple.*the" test.txt
It is the apple
> grep "the" test.txt | grep "apple" test.txt
It is the apple

"lo"๋กœ ์‹œ์ž‘ํ•œ 4๊ฐœ์˜ ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด๊ณผ "th"๋กœ ์‹œ์ž‘ํ•œ 1๊ฐœ์˜ ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์„ OR ์กฐ๊ฑด์œผ๋กœ ์‹คํ–‰

> grep -E "lo..|th." test.txt
the
thing
love
loving
It is the apple
> grep -e "lo.." -e "th." test.txt
the
thing
love
loving
It is the apple

 

4. FILE

์—ฌ๋Ÿฌ ํŒŒ์ผ์—์„œ grep ๋ช…๋ น์–ด ์‹คํ–‰

- ์ถœ๋ ฅ ๋ผ์ธ ์•ž์— ์–ด๋–ค ํŒŒ์ผ์— ํฌํ•จ๋˜์–ด์žˆ๋Š”์ง€ ํ‘œ์‹œ

> grep a A.txt B.txt
A.txt:"Grant me the serenity to accept the things I can not change,
A.txt:the courage to change the things I can,
A.txt:and wisdom to know the difference"
A.txt:-Karl Paul Reinhold Niebuhr-
B.txt:If it's bad, it's experience."
B.txt:-Victoria Holt-

 

ํ•ด๋‹น ๋””๋ ‰ํ† ๋ฆฌ ๋ชจ๋“  ํŒŒ์ผ์—์„œ grep ๋ช…๋ น์–ด ์‹คํ–‰ 

- ์ถœ๋ ฅ ๋ผ์ธ ์•ž์— ์–ด๋–ค ํŒŒ์ผ์— ํฌํ•จ๋˜์–ด์žˆ๋Š”์ง€ ํ‘œ์‹œ

- ํ™•์žฅ์ž ๋ณ„๋„ ์ง€์ • ๊ฐ€๋Šฅ

> grep a *
A.txt:"Grant me the serenity to accept the things I can not change,
A.txt:the courage to change the things I can,
A.txt:and wisdom to know the difference"
A.txt:-Karl Paul Reinhold Niebuhr-
B.txt:If it's bad, it's experience."
B.txt:-Victoria Holt-
grep: Workspace: Is a directory
> grep a *.txt
A.txt:"Grant me the serenity to accept the things I can not change,
A.txt:the courage to change the things I can,
A.txt:and wisdom to know the difference"
A.txt:-Karl Paul Reinhold Niebuhr-
B.txt:If it's bad, it's experience."
B.txt:-Victoria Holt-

 

5. ์‚ฌ์šฉํ•˜๊ธฐ

ํ˜„์žฌ ์‹คํ–‰๋œ ํ”„๋กœ์„ธ์Šค ์ค‘ "Wireshark" ๋ฌธ์ž์—ด์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅ

> ps -ax | grep -i wireshark
12334 ??         0:01.71 /Applications/Eve Wireshark.app/Contents/MacOS/Wireshark
12369 ??         0:00.08 /Applications/Eve Wireshark.app/Contents/MacOS/dumpcap -S -Z none
12383 ttys000    0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox -i wireshark