>  기사  >  php教程  >  Grep command-9 클래식 사용 시나리오에 대한 자세한 설명

Grep command-9 클래식 사용 시나리오에 대한 자세한 설명

高洛峰
高洛峰원래의
2016-12-13 14:54:101666검색

Grep
은 Global Regular Expression Print의 약어로 전역 정규식을 의미합니다.
는 정규 매칭
1. 명령 형식
grep [options] 파일
을 사용하는 강력한 텍스트 검색 도구입니다.

2. 주요 매개변수
-c: 일치하는 행 수만 출력
-i: 대소문자 구분 없음
-n: 일치하는 탐색 및 행 번호 표시
-l: 여러 파일을 질의할 경우 일치하는 문자가 포함된 파일 이름만 출력됩니다.
-v: 역방향 일치, 즉 일치하지 않는 행이 표시됩니다.
-h: 질의 시 파일 이름을 적용할 수 없습니다.
-s: 하지 않음 오류 메시지 표시


3. 정규식의 일부
반의어 문자: """"와 같은 의미는 ""
^$ 시작 및 끝
[] 단일 문자 일치를 의미합니다. , [ A]
[ - ]는 범위와 일치하고, [0-9a-zA-Z]는 모든 숫자 및 문자와 일치합니다.
* 이전 문자가 0번 이상 나타남
+ 이전 문자가 한 번 나타남 또는 여러 번
. 모든 문자

4. 클래식 장면

대소문자를 정확하게 구분하고 싶지 않은 경우 대소문자를 무시하려면 -i를 추가하세요

(1) find 명령과 파이프라인 결합
음악 폴더에 여러 형식의 파일이 있고 아티스트 Jay의 mp3 파일만 찾으려고 하며 혼합된 오디오 트랙은 포함되어 있지 않습니다
[root@localhost ~]#find . -name ".mp3" | grep -i jay | grep -vi "remix"
분석: 1) find -name을 사용하여 모든 mp3 파일을 나열하고 grep으로 리디렉션합니다. 2) grep -i를 사용하여 jay가 포함된 라인 찾기
3) grep -vi를 사용하여 remix가 포함되지 않은 라인 찾기


(2)-A -B -C
다수 때때로 우리는 일치하는 줄에 관심이 없지만 일치하는 줄의 컨텍스트에 관심이 있습니다. 이때 -A ​​-B -C가 유용합니다
-A n n행 이후, A는 (After)로 기억됩니다
-B n n행 이전, B는 (Before)
-C n 이전으로 기억됩니다 n 라인, 다음 n 라인, C 메모리는 (가운데)

[root@localhost ~]# ifconfig | grep -A 2 "Link encap"
eth0      Link encap:Ethernet  HWaddr 00:0C:29:F3:38:15  
          inet addr:192.168.91.129  Bcast:192.168.91.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fef3:3815/64 Scope:Link
--
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host


[root@localhost ~]#  ifconfig | grep -C 2 "lo"
          Interrupt:67 Base address:0x2024 


lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
(3) -c를 사용하여 숫자를 계산합니다

이 파일이 대용량입니다. www.baidu.com Tieba.baidu.com 등과 같은 URL이 포함되어 있습니다. Baidu에 속하는 URL 수를 알고 싶으십니까
[

root@localhost ~]# grep -c "*baidu.com*" filename
例子
[root@localhost ~]# cat file.txt
wtmp begins Mon Feb 24 14:26:08 2014
192.168.0.1
162.12.0.123
"123"
123""123
njuhwc@163.com
njuhwc@gmil.com 123
www.baidu.com
tieba.baidu.com
www.google.com
www.baidu.com/search/index
[root@localhost ~]# grep -cn ".*baidu.com.*" file.txt 
3
(4) -r 하위 디렉터리를 재귀적으로 검색

현재 디렉터리와 하위 디렉터리에서 일치하는 문자가 포함된 파일 찾기
하위 디렉토리를 검색하고 일치 후 줄 번호를 출력합니다.

[root@localhost ~]# grep -nr HELLO_HWC_CSND_BLOG* .

예:

[root@localhost ~]# grep -nr baidu .
./file.txt:8:www.baidu.com
./file.txt:9:tieba.baidu.com
./file.txt:11:www.baidu.com/search/index
./test/test.txt:1:http://www.baidu.com
하위 디렉터리를 검색하고 일치한 후 파일 이름만 출력합니다.

[root@localhost ~]# grep -lr HELLO_HWC_CSND_BLOG* .

예:

[root@localhost ~]# grep -lr baidu .
./file.txt
./test/test.txt
(5)--line-buffered 버퍼링 모드를 켭니다.

동적 파일이 있고 파일 끝에 지속적으로 정보를 추가하며 특정 정보가 포함된 줄을 출력하려고 합니다. 즉, 동적 스트림


[root@localhost ~]#tail -f file | grep --line-buffered your_pattern


(6)을 지속적으로 grep합니다. ps 프로세스 찾기

[root@localhost ~]# ps aux | grep init
root         1  0.0  0.1   2072   632 ?        Ss   22:52   0:01 init [5]                             
root      4210  0.0  0.1   6508   620 ?        Ss   23:01   0:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "/usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients"
root      4233  0.0  0.0   2780   504 ?        S    23:01   0:00 /usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients
root      4956  0.0  0.1   3920   680 pts/1    R+   23:27   0:00 grep init
여기서 grep init로 실행한 명령도 나열되어 있음을 알 수 있습니다

이 줄을 원하지 않으면 다음과 같이 명령을 변경할 수 있습니다

[root@localhost ~]# ps aux | grep [i]nit
root         1  0.0  0.1   2072   632 ?        Ss   22:52   0:01 init [5]                             
root      4210  0.0  0.1   6508   620 ?        Ss   23:01   0:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "/usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients"
root      4233  0.0  0.0   2780   504 ?        S    23:01   0:00 /usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients
( 7) 특정 디렉터리를 포함하지 않는 디렉터리 검색

[root@localhost ~]#grep -R --exclude-dir=node_modules 'some 패턴' /path/to/search

[root@localhost ~]# ls
anaconda-ks.cfg  Desktop  file.txt  find.result  install.log  install.log.syslog  test
[root@localhost ~]# grep -r baidu .
./file.txt:www.baidu.com
./file.txt:tieba.baidu.com
./file.txt:www.baidu.com/search/index
./test/test.txt:http://www.baidu.com
지금 테스트 디렉터리를 포함하지 않으려는 경우

[root@localhost ~]# grep -R --exclude-dir=text "baidu" .
./file.txt:www.baidu.com
./file.txt:tieba.baidu.com
./file.txt:www.baidu.com/search/index
오류가 보고되는 경우

grep: unrecognized option `--exclude-dir=test'
버전이 너무 오래되었다는 뜻이므로 업데이트하면 괜찮습니다


(8) IP 주소 찾기

-o 및 - 여기서는 P 명령어를 사용합니다
man grep을 통해 확인합니다
-o, --only-matching:
PATTERN과 일치하는 일치하는 라인 부분만 표시합니다.
-P, --perl- regexp:
PATTERN을 Perl 정규식으로 해석합니다.
즉, -o는 일치하는 줄에서만 일치 항목을 표시합니다. 정규식
-P의 일부, Perl 정규식 일치

[root@localhost ~]# cat file.txt
wtmp begins Mon Feb 24 14:26:08 2014
192.168.0.1
162.12.0.123
"123"
123""123
njuhwc@163.com
njuhwc@gmil.com 123
www.baidu.com
tieba.baidu.com
www.google.com
www.baidu.com/search/index
[root@localhost ~]# grep -oP "([0-9]{1,3}\.){3}[0-9]{1,3}" file.txt
192.168.0.1
162.12.0.123
(9) 메일함 찾기

[root@localhost ~]# grep -oP "[a- zA-Z0-9_-]+@[a-zA-Z0-9_-] +(.[a-zA-Z0-9_-]+)+" file.txt


[root@localhost ~]# cat file.txt
wtmp begins Mon Feb 24 14:26:08 2014
192.168.0.1
162.12.0.123
"123"
123""123
njuhwc@163.com
njuhwc@gmil.com 123
www.baidu.com
tieba.baidu.com
www.google.com
www.baidu.com/search/index
[root@localhost ~]# grep -oP "[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+" file.txt
njuhwc@163.com
njuhwc@gmil.com


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:강력한 grep 명령다음 기사:강력한 grep 명령