[Shell] 05. sed
카테고리: BASH
태그: bash
01) sed
sed는 스트림 에디터로, 입력을 받아서 정규표현식이나 패턴에 따라 치환, 삭제, 추가 등의 작업을 수행할 수 있다.
a) 명령 형식
sed [option] 'instructions' filename
| 옵션 | 설명 |
|---|---|
| -n | 입력줄 자동 출력을 제한하기 위해 사용 |
| -e | 하나 이상의 지시어를 사용할 경우 사용 |
| -i | 직접적 파일 편집을 위해 사용 |
| -f | sed 지시어 스크립트 파일을 사용하기 위해 사용 |
b) 출력 예제
# 01. selinux 파일의 22번째 줄을 출력
$ sed -n '22p' /etc/sysconfig/selinux
SELINUX=enforcing
# 02. 7번째 줄부터 마지막 줄까지 내용을 출력
$ sed -n '22,$p' /etc/sysconfig/selinux
SELINUX=enforcing
# SELINUXTYPE= can take one of these three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
# 03. 여러 줄의 범위를 출력해야 할 경우, 지시어의 구분자는 세미콜론(;)을 사용
$ sed -n '2p;5,6p;22p' /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=enforcing
# 04. 다른 형식으로도 사용할 수 있다.
$ sed -n '
> 2p
> 5,6p
> 22p' /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=enforcing
# 05. 스크립트 파일을 참고하여 처리하기
$ cat test.sed
#n
2p
5,6p
22p
$ sed -f test.sed /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=enforcing
c) 삭제 예제
# 01. enforcing과 빈 줄을 포함하고 있는 줄을 제거
$ sed '/enforcing/d;/^$/d' /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# disabled - No SELinux policy is loaded.
#
# NOTE: In earlier Fedora kernel builds, SELINUX=disabled would also
# fully disable SELinux during boot. If you need a system with SELinux
# fully disabled instead of SELinux running with no policy loaded, you
# need to pass selinux=0 to the kernel command line. You can use grubby
# to persistently set the bootloader to boot with selinux=0:
#
# grubby --update-kernel ALL --args selinux=0
#
# To revert back to SELinux enabled:
#
# grubby --update-kernel ALL --remove-args selinux
#
# SELINUXTYPE= can take one of these three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
# 02. 7번째 줄부터 10번째 줄을 삭제
$ sed '1,20d' /etc/sysconfig/selinux
# SELINUXTYPE= can take one of these three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
d) 치환 예제
# 지시어 형식 : '[줄 범위]s/패턴/변경값/플래그'
# 01. 첫 번째 줄부터 다섯 번째 줄까지 내용 중 줄의 첫 부분 # 문자를 * 문자로 치환
$ sed '1,5s/^#/\*/' /etc/sysconfig/selinux | head -n 6
* This file controls the state of SELinux on the system.
* SELINUX= can take one of these three values:
* enforcing - SELinux security policy is enforced.
* permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
# 02. enforcing을 포함하고 있는 줄의 첫 부분 # 문자를 * 문자로 치환
$ sed '/enforcing/s/^#/\*/' /etc/sysconfig/selinux | head -n 6
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
* enforcing - SELinux security policy is enforced.
* permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
# 03. 한 줄에 변경하고자 하는 내용이 두 개 이상 있는 경우
$ cat 1.txt
cat tiger cat
# 한 줄에 변경하고자 하는 내용이 두 개 이상 있는 경우
$ sed 's/cat/lion/' 1.txt
lion tiger cat
# 줄 전체에 적용하기 위해 플래그 위치에 g를 사용
$ sed 's/cat/lion/g' 1.txt
lion tiger lion
# 04. 원본 파일에 바로 적용하고자 한다면 -i 옵션을 사용
$ grep -n '^SELINUX=' /etc/sysconfig/selinux
20:SELINUX=enforcing
$ sed -i 's/SELINUX=enforcing/SELINUX=permissive/' /etc/sysconfig/selinux
$ grep -n '^SELINUX=' /etc/sysconfig/selinux
20:SELINUX=permissive
e) 삽입/추가/변경
sed '줄번호 명령\문자열' 파일명
sed '/패턴/ 명령\문자열' 파일명
| 명령 | 설명 |
|---|---|
| i | 명시된 줄번호 또는 패턴에 일치하는 줄의 윗줄에 문자열을 삽입(Insert) 한다. |
| a | 명시된 줄번호 또는 패턴에 일치하는 줄의 아랫줄에 문자열을 추가(Append) 한다. |
| c | 명시된 줄번호 또는 패턴에 일치하는 줄의 내용을 문자열로 변경(Change) 한다. |
# 01
$ grep -n '^#net.ipv4.ip_forward' /etc/sysctl.conf
28:#net.ipv4.ip_forward=1
# /etc/sysctl.conf 파일의 28번째 줄의 주석을 제거하고, 그 윗줄에 #net.ipv4.ip_forward = 1을 삽입
$ cat i.sed
28s/#net.ipv4.ip_forward/net.ipv4.ip_forward/
28 i\#net.ipv4.ip_forward=1
$ sed -f i.sed /etc/sysctl.conf | head -n 30
~(생략)~
# Uncomment the next line to enable packet forwarding for IPv4
#net.ipv4.ip_forward=1
net.ipv4.ip_forward=1
# 02
$ cat a.sed
28s/#net.ipv4.ip_forward/net.ipv4.ip_forward/
28 a\#net.ipv4.ip_forward=1
$ sed -f a.sed /etc/sysctl.conf | head -n 30
~(생략)~
# Uncomment the next line to enable packet forwarding for IPv4
net.ipv4.ip_forward=1
#net.ipv4.ip_forward=1
# 03
$ cat c.sed
28 c\#net.ipv4.ip_forward=0
$ sed -f c.sed /etc/sysctl.conf | head -n 30
~(생략)~
# Uncomment the next line to enable packet forwarding for IPv4
#net.ipv4.ip_forward=0
댓글 남기기