본문 바로가기

IaC Application/SHELL

정규표현식 - grep

[root@localhost ~]# ifconfig
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.0.120  netmask 255.255.255.0  broadcast 172.16.0.255
        inet6 fe80::20c:29ff:fe78:c387  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:78:c3:87  txqueuelen 1000  (Ethernet)
        RX packets 98223  bytes 17291818 (16.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 175785  bytes 20733836 (19.7 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 18  bytes 2112 (2.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 18  bytes 2112 (2.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

--inet이 들어간 행 출력
[root@localhost ~]# ifconfig | grep inet
        inet 172.16.0.120  netmask 255.255.255.0  broadcast 172.16.0.255
        inet6 fe80::20c:29ff:fe78:c387  prefixlen 64  scopeid 0x20<link>
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
[root@localhost ~]# ifconfig | grep -c inet
4

--127.0.0.1이 들어간 행 제외하고 출력
[root@localhost ~]# ifconfig | grep -v 127.0.0.1
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.0.120  netmask 255.255.255.0  broadcast 172.16.0.255
        inet6 fe80::20c:29ff:fe78:c387  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:78:c3:87  txqueuelen 1000  (Ethernet)
        RX packets 98229  bytes 17292475 (16.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 175790  bytes 20734190 (19.7 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 18  bytes 2112 (2.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 18  bytes 2112 (2.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

--행을 /root/if-grep.txt로
[root@localhost ~]# ifconfig | grep -v 127.0.0.1 > /root/if-grep.txt

 

regex

 

t로 시작하는 문자열 검색

[root@localhost ~]# grep "^t" /root/regex01.txt 
test01
test02
tcpip4layer
tcp4

 

r로 끝나는 문자열을 검색

[root@localhost ~]# grep "r$" /root/regex01.txt 
osi7layer
tcpip4layer

 

4글자 중 시작과 끝이 t, 가운데 두글자는 임의의 문자

 

[root@localhost ~]# grep "t..t" /root/regex01.txt 
test01
test02

 

t 뒤에 e 가 0개 이상 있을 때

[root@localhost ~]# grep "te*" /root/regex01.txt 
test01
test02
Test03
tcpip4layer
tcp4
[centos-redhat]
www.daum.net

 

시작 시 문자가 A-Z(대문자) 중 하나인 행을 검색

[root@localhost ~]# grep "^[A-Z]" /root/regex01.txt 
This is regular expression sample
Test03

 

숫자 0-9로 끝나는 행을 검색

[root@localhost ~]# grep "[0-9]$" /root/regex01.txt 
test01
test02
Test03
osi7
tcp4

 

t e s 중 1개라도 들어있는 행을 검색

[root@localhost ~]# grep "[tes]" /root/regex01.txt 
This is regular expression sample
test01
test02
Test03
osi7layer
tcpip4layer
osi7
tcp4
bef
cdef
s
ws
wws
wwws
wss
wsss
wsp
wsssp
wswsp
[centos-redhat]
[kali linux-debian]
[solaris8]
www.google.com
www.daum.net

 

소문자가 아닌것으로 시작되는 행을 검색

[root@localhost ~]# grep "^[^a-z]" /root/regex01.txt 
This is regular expression sample
Test03
[centos-redhat]
[kali linux-debian]
[WINDOWS]
[solaris8]
.com

 

[root@localhost ~]# grep "test|daum" /root/regex01.txt 
[root@localhost ~]# egrep "test|daum" /root/regex01.txt 
test01
test02
www.daum.net

 

a b c 가 아닌 문자열 검색

[root@localhost ~]# grep "^[^a|b|c]" /root/regex01.txt 
This is regular expression sample
test01
test02
Test03
osi7layer
tcpip4layer
osi7
tcp4
s
ws
wws
wwws
wss
wsss
wsp
wsssp
wswsp
[centos-redhat]
[kali linux-debian]
[WINDOWS]
[solaris8]
www.google.com
www.daum.net
.com

[root@localhost ~]# grep "(osi7|tcpip4)layer" /root/regex01.txt 
[root@localhost ~]# egrep "(osi7|tcpip4)layer" /root/regex01.txt 
osi7layer
tcpip4layer

[root@localhost ~]# egrep "ab?" /root/regex01.txt 
This is regular expression sample
osi7layer
tcpip4layer
ab
abb
abbb
abbbb
abbbbb
abc
ababc
aabbabc
[centos-redhat]
[kali linux-debian]
[solaris8]
www.daum.net

[root@localhost ~]# egrep "ab+" /root/regex01.txt 
ab
abb
abbb
abbbb
abbbbb
abc
ababc
aabbabc

앞에 문자열이 존재하는 .com이나 .net만 검색

 

greedy

 

lazy

'IaC Application > SHELL' 카테고리의 다른 글

함수  (0) 2024.03.21
SHELL  (0) 2024.03.21