본문 바로가기

OS

[Linux][Command] grep

반응형

 Enviroment

- Linux/MaxOS
- Bash shell(/bin/bash)

 

 Synopsis

grep 명령어는 Linux 및 유닉스 기반 시스템에서 텍스트 검색과 패턴 매칭을 수행하는 데 사용됩니다.

 

option description example
-A num, --after-context=num Print num lines of trailing context after each match. grep -A 5
-a, --text Treat all files as ASCII text. grep -a
-B num, --before-context=num Print num lines of leading context before each match. grep -B 5
-b, --byte-offset The offset in bytes of a matched pattern is displayed in front of the respective matched line. grep -b
-C num, --context=num Print num lines of leading and trailing context surrounding each match. grep -C 5
-c, --count Only a count of selected lines is written to standard output. grep -c
-I Ignore binary files.  This option is equivalent to the “--binary-files=without-match” option. grep -l
-i, --ignore-case Perform case insensitive matching.  By default, grep is case sensitive. grep -i
--include pattern If specified, only files matching the given filename pattern are searched. Note that --include and --exclude patterns are processed in the order given. If a name matches multiple patterns, the latest matching rule wins.  Patterns are matched to the full path specified, not only to the filename component. grep --include "abc"
--include-dir pattern If -R is specified, only directories matching the given filename pattern are searched. grep --include-dir "abc"
-m num, --max-count=num Stop reading the file after num matches. grep -m 5
-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. grep -n
-O If -R is specified, follow symbolic links only if they were explicitly listed on the command line. grep -O
-o, --only-matching Prints only the matching part of the lines. grep -o
-q, --quiet, --silent Quiet mode: suppress normal output. grep -q
-R, -r, --recursive Recursively search subdirectories listed. grep -R
-S If -R is specified, all symbolic links are followed. grep -S
-s, --no-messages Silent mode.  Nonexistent and unreadable files are ignored (i.e., their error messages are suppressed). grep -s
-w, --word-regexp The expression is searched for as a word. grep -w
-x, --line-regexp Only input lines selected against an entire fixed string or regular expression are considered to be matching lines. grep -x
-y Equivalent to -i.  Obsoleted. grep -y
-z, --null-data Treat input and output data as sequences of lines terminated by a zero-byte instead of a newline. grep -z
-X, --xz Decompress the xz(1) compressed file before looking for the text. grep -X
--line-buffered Force output to be line buffered.
grep --line-buffered

 

 Usage

'patricia' 패턴을 가진 모든 발생을 파일에서 찾기

$ grep 'patricia' myfile

단어로서의 'patricia' 패턴을 찾기

$ grep -w 'patricia' myfile

정확한 패턴 'FOO'의 발생 횟수 세기

$ grep -c FOO myfile

대소문자 무시하고 'FOO' 패턴의 발생 횟수 세기

$ grep -c -i FOO myfile

라인 시작에서 '.Pp' 패턴 찾기

$ grep '^\.Pp' myfile

'foo' 또는 'bar' 단어를 포함하지 않는 라인 찾기

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

확장 정규 표현식을 사용하여 'calendar' 파일에서 19, 20, 또는 25 찾기

$ egrep '19|20|25' calendar

'FIXME' 패턴이 포함된 '*.h' 파일과 해당 라인 출력 (재귀적으로 검색)

$ grep -H -R FIXME --include="*.h" /usr/src/sys/arm/

'FIXME' 패턴이 포함된 '*.h' 파일의 이름만 출력 (재귀적으로 검색)

$ grep -l -R FIXME --include="*.h" /usr/src/sys/arm/

'foo' 텍스트를 포함하는 라인 출력 (색상 및 라인 번호 포함)

$ grep -b --colour -n foo myfile

확장 정규 표현식 패턴을 사용하여 라인 버퍼링 강제로 설정

$ grep --line-buffered 'pattern' myfile

'error' 패턴 이후에 나오는 2줄을 추가로 표시하기

$ grep -A 2 'error' logfile.txt

 

 

usage: grep [-abcdDEFGHhIiJLlMmnOopqRSsUVvwXxZz] [-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 ...]
반응형

'OS' 카테고리의 다른 글

[Linux] scp command  (0) 2022.09.09