grep – 파일에서 원하는 조건의 라인 찾기
grep 는 특정파일에서 원하는 조건에 해당하는 라인을 찾을때 사용하는 명령어
0 1 2 3 4 5 6 7 8 9 |
# filename.txt 에서 패턴을 제외한 라인 찾기 grep -v '패턴' filename.txt # filename.txt 에서 패턴1,패턴2,패턴3이 있는 라인 찾기 grep -E '패턴1|패턴2|패턴3' filename.txt # filename.txt 에서 패턴1,패턴2,패턴3을 제외한 라인 찾기 grep -Ev '패턴1|패턴2|패턴3' filename.txt |
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
[root@server ~]# grep --help 사용법: grep [옵션]... 패턴 [파일]... Search for PATTERN in each FILE or standard input. PATTERN is, by default, a basic regular expression (BRE). Example: grep -i 'hello world' menu.h main.c Regexp selection and interpretation: -E, --extended-regexp PATTERN is an extended regular expression (ERE) -F, --fixed-strings PATTERN is a set of newline-separated fixed strings -G, --basic-regexp PATTERN is a basic regular expression (BRE) -P, --perl-regexp PATTERN is a Perl regular expression -e, --regexp=PATTERN use PATTERN for matching -f, --file=FILE obtain PATTERN from FILE -i, --ignore-case ignore case distinctions -w, --word-regexp force PATTERN to match only whole words -x, --line-regexp force PATTERN to match only whole lines -z, --null-data a data line ends in 0 byte, not newline Miscellaneous: -s, --no-messages suppress error messages -v, --invert-match select non-matching lines -V, --version display version information and exit --help display this help text and exit Output control: -m, --max-count=NUM stop after NUM matches -b, --byte-offset print the byte offset with output lines -n, --line-number print line number with output lines --line-buffered flush output on every line -H, --with-filename print the file name for each match -h, --no-filename suppress the file name prefix on output --label=LABEL use LABEL as the standard input file name prefix -o, --only-matching show only the part of a line matching PATTERN -q, --quiet, --silent suppress all normal output --binary-files=TYPE assume that binary files are TYPE; TYPE is 'binary', 'text', or 'without-match' -a, --text equivalent to --binary-files=text -I equivalent to --binary-files=without-match -d, --directories=ACTION how to handle directories; ACTION is 'read', 'recurse', or 'skip' -D, --devices=ACTION how to handle devices, FIFOs and sockets; ACTION is 'read' or 'skip' -r, --recursive like --directories=recurse -R, --dereference-recursive likewise, but follow all symlinks --include=FILE_PATTERN search only files that match FILE_PATTERN --exclude=FILE_PATTERN skip files and directories matching FILE_PATTERN --exclude-from=FILE skip files matching any file pattern from FILE --exclude-dir=PATTERN directories that match PATTERN will be skipped. -L, --files-without-match print only names of FILEs containing no match -l, --files-with-matches print only names of FILEs containing matches -c, --count print only a count of matching lines per FILE -T, --initial-tab make tabs line up (if needed) -Z, --null print 0 byte after FILE name Context control: -B, --before-context=NUM print NUM lines of leading context -A, --after-context=NUM print NUM lines of trailing context -C, --context=NUM print NUM lines of output context -NUM same as --context=NUM --group-separator=SEP use SEP as a group separator --no-group-separator use empty string as a group separator --color[=WHEN], --colour[=WHEN] use markers to highlight the matching strings; WHEN is 'always', 'never', or 'auto' -U, --binary do not strip CR characters at EOL (MSDOS/Windows) -u, --unix-byte-offsets report offsets as if CRs were not there (MSDOS/Windows) |
[table caption=”Linux grep Option” colwidth=”100|200″ colalign=”center|center|left”]
option , 풀이 , 설명
-A NUM , –after-context=NUM , 패턴매칭라인 이후의 라인을 NUM수만큼 출력
-a , –text , 기본적으로 grep는 바이너리 파일을 처리할 수없다. 그런데 바이너리파일을 텍스트파일처럼 처리할 수있는 옵션이 -a 옵션이다
-B NUM , –before-context=NUM , 패턴매칭라인 이전의 내용을 NUM수만큼 출력.
-b , –byte-offset , 패턴매칭되기전 라인의 바이트수를 출력
-C NUM , –context=NUM , 출력물 앞뒤 전후의 주어진 라인만큼 출력(패턴매칭 라인은 포함하지 않고 기본 2라인)
-c, –count , 파일의 내용 대신 문자열이 들어있는 라인의 수를 출력(3개의 라인에서 매칭된다면 3출력)
-E , –extended-regexp , 확장 정규표현식으로 패턴을 해석한다(=egrep 같은 의미)
-e PATTERN , –regexp=PATTERN , pattern 에서 찾을 문자열 명시 / 패턴으로 PATTERN을 사용(“-으로 시작하는 패턴”을 보호하는데 유용)
-F , –fixed-strings , newline으로 분리된 고정된 문자열의 리스트로써 패턴을 해석한다(=fgrep과 같은 의미)
-f FILE , –file=FILE , 파일로부터 패턴을 얻는다(파일의 라인마다). 공백파일은 zero 패턴을 포함한다. 그러므로 매치되는것이 없다.
-G , –basic-regexp , 정규표현식으로써 PATTERN을 해석하고 이것이 기본이다
-H , –with-filename , 매칭되는 파일명을 출력
-h , –no-filename ,여러개의 파일이 검색되었을때 -H 옵션처럼 앞에 파일명을 출력하지 않는다 –help / help 메세지를 출력
-I(대문자 i) , , 매칭된 데이터를 포함하지 않는것처럼 바이너리 파일을 처리한다. binary-files=without-match 옵션과 동등하다
-i , –ignore-case , 주어진 패턴이나 입력파일에서 대소문자를 무시한다.
-L , –files-without-match , 일반적으로 출력되는 출력을 스킵하고 각각의 입력파일을 출력한다. 처음으로 매칭되는 파일이 나타나면 중지된다
-l , –files-with-matches , -L 과 반대
-n , –line-number , 문자열이 들어있는 라인과 문두에 라인번호를 출력
-r , –recursive , 하위 디렉토리를 순환적으로 모두 읽는다. -d recurse 옵션과 동등하다
-V , –version , 표준에러로 grep의 버전을 출력한다.
-v , –invert-match , 문자열이 포함되어 있지 않은 라인을 출력
-w , –word-regexp , pattern 이 전체 단어와 일치하는 줄만 출력 / 단어의 일부로써 일치하는 경우가 아닌 하나의 단어로써 일치하는 줄이 출력.
-x , –line-regexp , pattern 이 전체 줄과 일치하는 줄만 출력
[/table]
[table caption=”Linux grep 정규표현식” colwidth=”100|150|100″ colalign=”center|left|center|left”]
메타문자,기 능,사용 예,사용 예 설명
^ , 행의 시작 지시자 , ‘^love’ , love로 시작하는 모든 행과 대응
$ , 행의 끝 지시자 , ‘love$’ , love로 끝나는 모든 행과 대응
. , 하나의 문자와 대응 , ‘l..e’, l 다음에 두 글자가 나오고 e로 끝나는 문자열을 포함하는 행과 대응
* , 선행문자와 같은 문자의 0개 혹은 임의개수와 대응 , ‘ *love’ , 0개 혹은 임의 개수의 공백 문자 후에 love로 끝나는 문자열을 포함한 행과 대응(*앞에 공백이 있는 상태 / 다른 문자가 있다면 그 문자가 0개 혹은 임의 개수의 문자 후에 love로 끝나느 경우를 말함)
[] , [] 사이의 문자 집합중 하나와 대응 , ‘[Ll]ove’ , love나 Love를 포함하는 행과 대응
[^ ] , 문자집합에 속하지 않는 한 문자와 대응 , ‘[^A-K]love’ , A와 K 사이의 범위에 포함되지 않는 한 문자와 ove가 붙어있는 문자열과 대응
\< , 단어의 시작 지시자 , ‘\<love’ , love로 시작하는 단어를 포함하는 행과 대응(vi / grep에서 지원)
\> , 단어의 끝 지시자 , ‘love\>’ , love로 끝나는 단어를 포함하는 행과 대응(vi / grep에서 지원)
\(..\) , 다음 사용을 위해 태그를 붙인다. , ‘\(lov\)ing’ , 지정된 부분을 태크1에 저장한다. 나중에 태그값을 참고하려면 \1을 쓴다. 맨 왼쪽부터 시작해 태그를 9개가지 쓸 수 있다. 왼쪽 예에서는 lov가 레지스터1에 저장되고 나중에 \1로 참고할 수 있다.
x\{m\} , 문자 x를 m번 반복한다. , ‘o\{5\}’ , 문자 o가 5회 연속적으로 나오는 모든 행과 대응
[/table]