본문 바로가기
Managing Servers/Learn to live in Terminal

Deep Dive Grep

by 단북 2020. 7. 8.

# deep_grep(파일 이름)

I'm Lil System Engineer aka.alinos
I Like Grep
ex) hi hello grep
1. grep
2. Case insensitive -i
3. recursively -r/-R
4. word -w
5. count -c
6. line number -n
7. invert match -v
8. Egrep -E -e
9. After/Before/aroound -A -B -C
10. Grep color --color=auto
11. grep word in file name -l -L
12. only output -o
13. position byte -b
14. Regular expression

 

1. grep default

  • 문서에서 찾고 싶은 문자열을 검색할 때 사용한다.
# grep grep deep_grep
ex) hi hello grep
1. grep
8. Egrep -E -e
11. grep word in file name -l -L

2. Case insensitive grep

  • 문서에서 대소문자 가리지 않고 문자열을 검색할 때 사용한다. 
# grep -i grep deep_grep
I Like Grep
ex) hi hello grep
1. grep
8. Egrep -E -e
10. Grep color --color=auto
11. grep word in file name -l -L

3. recursive/dereference-recursive grep

  • 리눅스 트리 구조에서 하위 디렉터리까지 포함하여 특정 문자열이 있는 문서를 검색한다.
  • -R(dereference-recursive) 옵션을 사용하면 include/exclude 같은 패턴 옵션을 사용할 수 있는데, 특정 디렉터리나 파일들을 제외할 수 있기 때문에 찾는 범위를 조금 더 포커싱할 수 있고, 사용함으로써 검색 속도나 I/O 부하를 효율적으로 줄일 수 있다.
# grep -r grep * 
deep_grep:ex) hi hello  grep
deep_grep:1. grep
deep_grep:8. Egrep -E -e
deep_grep:11. grep word in file name -l -L
stream/aka:grep

# grep -R --exclude-dir=stream grep *
deep_grep:ex) hi hello  grep
deep_grep:1. grep
deep_grep:8. Egrep -E -e
deep_grep:11. grep word in file name -l -L

4. word-regexp grep

  • 기본적인 grep의 검색은 grep 이란 단어를 찾으면, Egrep, 1grep 등 하나의 단어에 grep이란 문자열이 포함되어 있어도 검색이 되지만, word-regexp grep 은 오로지 grep 이란 단어만 찾아 출력해준다.
    Egrep, 1grep 같은 단어는 출력하지 않는다.
# grep -w grep deep_grep
1. grep
11. grep word in file name -l -L

5. count grep

  • 내가 찾는 문자열이 파일 안에 몇 개나 있는지 카운팅 하여 개수를 출력해준다.
# grep -c grep deep_grep
4
#  grep -w -c grep deep_grep
3 

6. line number grep

  • 내가 찾는 문자열이 몇 번째 라인에 있는지 내용과 함께 출력해준다.
# grep -n line deep_grep
9:6. line number -n

7. invert match grep

  • 내가 찾는 문자열이 있는 라인을 제외하고 출력해준다.
  • 다음 예제에서 출력 제외된 문구는 7번째 줄로 문구는 `7. invert match -v`이다.
# grep -v invert deep_grep
I'm Lil System Engineer aka.alinos
I Like Grep
ex) hi hello test
1. grep
2. Case insensitive -i
3. recursively -r/-R
4. word -w
5. count -c
6. line number -n
8. Egrep -E -e
9. After/Before/aroound -A -B -C
10. Grep color --color=auto
11. grep word in file name -l -L
12. only output -o
13. position byte -b
14. Regular expression

8. extended regular expression grep (aka. grep)

 

  • 여러 문자열을 한 번에 검색할 수 있다.
  • 몰라서 못쓰면 안쓰럽다.
# grep -E 'count|position|output' deep_grep
5. count -c
12. only output -o
13. position byte -b

9. After/Before context grep

  • 내가 찾고 싶은 문자열 전/후의 라인을 출력해준다.
# grep -A2 word deep_grep
4. word -w
5. count -c
6. line number -n
--
11. grep word in file name -l -L
12. only output -o
13. position byte -b
# grep -B2 word deep_grep
2. Case insensitive -i
3. recursively -r/-R
4. word -w
--
9. After/Before/aroound -A -B -C
10. Grep color --color=auto
11. grep word in file name -l -L
# grep -C2 word deep_grep
2. Case insensitive -i
3. recursively -r/-R
4. word -w
5. count -c
6. line number -n
--
9. After/Before/aroound -A -B -C
10. Grep color --color=auto
11. grep word in file name -l -L
12. only output -o
13. position byte -b

10. color grep

  • 찾고 싶은 문자열에 색을 입혀 가독성이 좋아진다.
  • auto와 always는 약간의 차이가 있는데, auto는 Linux pipe로 넘어가면 색깔이 지워지기 때문에 여러 pipeline을 거쳐도 앞서 잡은 grep의 색깔이 유지되길 원한다면 always 옵션을 사용하면 된다.
  • GREP_COLORS 란 변수를 사용해주면, 색깔을 변경할 수도 있다.
# grep --color=auto grep deep_grep
ex) hi hello  grep
1. grep
8. Egrep -E -e
11. grep word in file name -l -L

# grep 1 deep_grep --color=always |grep grep --color=auto -n
1. grep
11. grep word in file name -l -L

# grep 1 deep_grep --color=always |GREP_COLORS="mt=01;32" grep grep --color=auto
1. grep
11. grep word in file name -l -L

11. files with/without match grep

  • 내가 검색하는 문자열을 가지고 있는 파일을 검색해준다.
# grep -r -l grep * # --files-with-matches
deep_grep
gfreij
stream/aka
stream1/trst

# grep -r -L grep * # --files-without-match
arp.py
arprequest.pyc
cdn_gslb
ciphertext
fff
for
list
ne
spdtest.sh
stream/stream.f
stream/mysecond.c
stream/stream_c
stream/stream.c
stream/mysecond.o
stream/Makefile
test
test.pl
test.py

12. Only matching grep

  • 내가 찾은 문자열만을 출력해준다.
  • 특정 문자열 카운팅 할 때 조합해서 쓰기 좋다.
# grep -o only deep_grep
only
# grep -o [Gg]rep deep_grep | sort -n | uniq -c
      4 grep
      2 Grep

13. Byte offset grep

  • 내가 찾은 문자열의 byte 위치를 표시해준다.
  • 큰 파일을 자를 때 참고용으로 쓸만할 것 같다.
# grep [Gg]rep deep_grep -b
35:I Like Grep
47:ex) hi hello  grep
66:1. grep
178:8. Egrep -E -e
226:10. Grep color --color=auto
254:11. grep word in file name -l -L

14. Regular Expression grep

  • 기본적인 정규식을 알고 문자열을 검색하면 뭐든 찾을 수 있다는 자신감이 생긴다.
  • Posix Character Class
[:alnum:] Alphanumeric characters ex) [a-zA-Z0-9]
[:alpha:] Alphabetic characters ex) [a-zA-Z]
[:ascii:] ASCII characters ex) [\x00-\x7F]
[:blank:] Space and tab ex) [ \t]
[:cntrl:] Control characters ex) [\x00-\x1F\x7F]
[:digit:] Digits ex) [0-9]
[:graph:] Visible characters ex) [\x21-\x7E]
[:lower:] Lowercase letters  ex) [a-z]
[:print:] Visible characters and spaces ex) [\x20-\x7E]
[:punct:] Punctuation and symbols. ex) [!"#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~]
[:space:] All whitespace characters, including line breaks ex) [ \t\r\n\v\f]
[:upper:] Uppercase letters ex) [A-Z]
[:word:] Word characters ex) [A-Za-z0-9_]
[:xdigit:] Hexadecimal digits ex) [A-Fa-f0-9]

. Matches any single character.
? The preceding item is optional and will be matched, at most, once.
* The preceding item will be matched zero or more times.
+ The preceding item will be matched one or more times.
{N} The preceding item is matched exactly N times.
{N,} The preceding item is matched N or more times.
{N,M} The preceding item is matched at least N times, but not more than M times.
 Represents the range if it’s not first or last in a list or the ending point of a range in a list.
^ Matches the empty string at the beginning of a line; also represents the characters not in the range of a list.
$ Matches the empty string at the end of a line.
\b Matches the empty string at the edge of a word.
\B Matches the empty string provided it’s not at the edge of a word.
\< Match the empty string at the beginning of word.
\> Match the empty string at the end of word.

 

  • IP 검색 [[:숫자:]]{첫번째에서 세번째까지}[쩜]
#grep -Eo '[[:digit:]]{1,3}[.][[:digit:]]{1,3}[.][[:digit:]]{1,3}[.][[:digit:]]{1,3}' dnsmasq.log 
8.8.4.4
8.8.8.8
112.171.134.114
8.8.8.8
8.8.4.4
  • single character 정규식. 은 모든 하나의 글자를 대체한다.
# grep dns.a.q -o dnsmasq.log 
dnsmasq
  • 정규식 s로 시작하는 모든 리눅스 계정을 검색하는 range 정규식
#grep "\<s.*\>:x" /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
smmsp:x:51:51::/var/spool/mqueue:/sbin/nologin
sensu:x:494:495:Sensu Monitoring Framework:/opt/sensu:/bin/false
  • 복합 정규식
    • grep -E "(reply|cached) reply 나 cached 인 구문을 잡아라
    • grep -E "(reply|cached)[[:space:]] 그리고 다음에는 공백이 있는 문자다.
    • [[:space:]].*cloudfront.net 공백 뒤에는 모든 문자를 대체하고 대신 cloudfront.net으로 끝난다.
    • cloudfront.net is 54[.](192|230)[.]37[.].*7$" cloudfron.net is 다음 54.192/230.37 대역을 잡아라 마지막엔 7로 끝나는 IP로.

 

# grep -E "(reply|cached)[[:space:]].*cloudfront.net is 54[.](192|230)[.]37[.].*7$"  dnsmasq.log
Mar 25 03:26:26 dnsmasq[27337]: reply d2oh4tlt9mrke9.cloudfront.net is 54.192.37.117
Mar 25 03:26:26 dnsmasq[27337]: reply d2oh4tlt9mrke9.cloudfront.net is 54.192.37.67
Mar 25 03:26:27 dnsmasq[27337]: reply d3ewslr5655zon.cloudfront.net is 54.192.37.187
Mar 25 03:30:38 dnsmasq[27337]: reply dy6tztwnb325w.cloudfront.net is 54.192.37.87
Mar 25 03:30:39 dnsmasq[27337]: reply d1l2s6adam5wqx.cloudfront.net is 54.192.37.17
Mar 30 20:05:47 dnsmasq[31960]: reply d3cajjoh3a1m9n.cloudfront.net is 54.192.37.127
Mar 31 01:36:25 dnsmasq[31960]: reply d3ouyz166y09uz.cloudfront.net is 54.192.37.167

원문: https://brunch.co.kr/@shirano3/1

 

Deep Dive Grep

SE 필수 습득 명령!! | 저는 시스템 관리 업무중 NIX 시스템 분석과 모니터링을 좋아합니다. 시스템 분석과 모니터링을 하려면 시스템 로그와 VFS의 정보에 필요한 부분만을 잘 쓸어담아야 하죠. �

brunch.co.kr