sed 명령어
sed는 스트림 에디터(stream editor)로서, 텍스트 파일을 처리하고 수정하는데 사용하는 명령어입니다.
기본적인 sed 사용법
sed [옵션] '명령어' 파일명
여기서, 명령어는 주로 패턴 매칭(pattern matching)을 통해 문자열을 찾고, 이에 대해 특정 동작을 수행하는 것입니다. 예를 들어, 다음 명령어는 sample.txt 파일에서 "hello" 문자열을 "hi"로 변경합니다.
sed 's/hello/hi/g' sample.txt
위 명령어에서 s는 substitute의 약자로, 문자열 대체를 수행하는 명령어입니다. 여기서 hello는 대체하고자 하는 패턴, hi는 대체할 문자열이며, g는 전체 문자열에서 패턴에 매칭되는 모든 문자열을 찾아 대체하라는 옵션입니다.
다른 sed 명령어의 예시로는 다음과 같습니다.
- 파일 내용 출력하기
sed 'p' sample.txt
- 특정 행 출력하기
sed -n '5p' sample.txt
- 특정 행 범위 내의 문자열 치환
sed '2,5 s/hello/hi/g' sample.txt
- 정규식을 이용한 패턴 매칭 및 삭제
sed '/hello/d' sample.txt
- 특정 패턴 다음에 문자열 추가
sed '/hello/a hi there' sample.txt
위와 같이 다양한 옵션을 사용하여 텍스트 파일을 처리하고 수정할 수 있습니다. sed는 텍스트 파일 처리와 수정에 유용한 도구 중 하나이며, 자세한 사용 방법과 옵션에 대한 정보는 man 페이지에서 확인할 수 있습니다.
공백문자 파이프라인으로 치환
sed -i "s/[[:space:]]/|/g" 1.tlb
sed -r "s/\s+/\|/g" 1.tlb > 1.tmp
![:alnum:] ![a-z A-Z 0-9], 모든 알파벳 문자와 숫자
![:alpha:] ![a-z A-Z], 모든 알파벳 문자
![:blank:] 스페이스 문자와 탭문자
![:cntrl:] 모든 컨트롤 문자들
![:digit:] 숫자, ![0-9]
![:graph:] 모든 visible 문자들
![:lower:] 알파벳 소문자, ![a-z]
![:print:] Non-control 문자
![:space:] 공백문자
![:upper:] 알파벳 대문자, ![A-Z]
![:xdigit:] 16진수 문자![0-9 a-f A-F]
'IfModule mime_module.*' 문자열 검색 후 'AddType application/x-httpd-php-source .phps' 추가
sed -i 's,\(IfModule mime_module.*\),\1\n\tAddType application/x-httpd-php .htm .html .php .ph .phtml .inc,g;' /usr/local/apache2/conf/httpd.conf
sed -i 's,\(IfModule mime_module.*\),\1\n\tAddType application/x-httpd-php-source .phps,g;' /usr/local/apache2/conf/httpd.conf
$ cat /usr/local/apache2/conf/httpd.conf
<IfModule mime_module>
AddType application/x-httpd-php-source .phps
AddType application/x-httpd-php .htm .html .php .ph .phtml .inc
#
# TypesConfig points to the file containing the list of mappings from
# filename extension to MIME-type.
...
sed -i 's/^\(dir .*\)$/# \1\ndir \/data/' /etc/redis/redis.conf
sed -i.backfile -re "s/^(mirrorlist(.*))/#\1/g" CentOS-Base.repo
sed -i.backfile -re "s/^#baseurl/baseurl/g" CentOS-Base.repo
sed -i.backfile -re "s/http:\/\/mirror.centos.org/https:\/\/mirror.kakao.com/g" CentOS-Base.repo
'리눅스' 카테고리의 다른 글
[리눅스] ubuntu에 bind 설치하기(install bind on ubuntu) (0) | 2023.01.14 |
---|---|
[리눅스] keepalived, haproxy 설치 및 설정하기 (0) | 2023.01.13 |
[리눅스] centos7의 기본 저장소(mirror site) 변경(yum repository) (0) | 2023.01.12 |
[리눅스] ubuntu에 haproxy를 설치하는 방법 (0) | 2023.01.12 |
[리눅스] php 8.1에서 mongodb 확장 모듈 설치하기 (0) | 2023.01.11 |