배우는 내용 : [ grep 으로 문자열 검색해보기]

 

문자열 검색

grep [옵션] <검색 패턴> <파일 이름>

 

검색패턴 bash 이용한 예제
( -n : 행 번호도 출력, -v : 검색패턴을 포함하지않는 행 출력)

$ grep bash /etc/passwd
root:x:0:0:root:/root:/bin/bash
ubuntu:x:1000:1000:Ubuntu:/home/ubuntu:/bin/bash

$ grep -n bash /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
34:ubuntu:x:1000:1000:Ubuntu:/home/ubuntu:/bin/bash

$ grep -nv bash /etc/passwd
2:daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
3:bin:x:2:2:bin:/bin:/usr/sbin/nologin
...

 

대소문자를 구별하지 않고 검색

$ grep -i system /etc/passwd
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
systemd-network:x:100:102:systemd Network Management,,,:/run/systemd:/usr/sbin/nologin

 

조합

/etc 디렉터리에 ne 이라는 글자를 포함하는 파일을 검색

$ ls /etc | grep ne
issue.net
kernel
needrestart
netconfig
...

 

/etc 디렉터리에 ne 이라는 글자를 정규표현식 ^ 을 이용하여 검색

$ ls /etc | grep '^ne'
needrestart
netconfig
netplan
...

+ Recent posts