SED 명령어는 Stream Editor의 약자로
파일 내용에 대하여 검색, 찾기, 치환, 삽입 또는 삭제 등의 기능을 가지고 있는 리눅스의 고급명령어
& 스트림 편집기.
SED Example / Sample Command
1. 특정 패턴 문자열 출력 (awk 조합) : html 파일 에서 특정 볼륨의 사용량만 출력
sample.html
<td><p style="text-align:left"><abbr title="C:\ClusterStorage\Volume1">Volume1 <span style="font-size:10px;color:orange">*</span></abbr><br><span style="font-size:10px;color:#BDBDBD;text-align:left"><abbr title="PhysicalSize: 4.88 TB | Allocated: 4.88 TB | Unallocated: ~0 Byte">Volume01 <span style="font-size:10px;color:orange">*</span></abbr></span></p></td> <td bgcolor="#ACFA58"><p style="color:#298A08">Online</p></td> <td><p>CSV</p></td> <td><p>DZ-VI-IP-HV138</p></td> <td><p>FC</p></td> <td><p>GPT</p></td> <td><p>CSVFS</p></td> <td><p>0</p></td> <td><p style="line-height:1.2">4.27<br><span style="font-size:10px"> TB</span></p></td> <td bgcolor=""><p style="line-height:1.2">632.6<br><span style="font-size:10px"> GB</span></p></td> |
Input
sed -n '/Volume1 /,/~/p' sample.html | sed -n '10p'
sed -n '/Volume1 /,/~/p' sample.html | sed -n '10p' | awk -F ">" '{print $3}' | awk -F "<" '{print $1}'
Output
632.6
2. 원하는 문자열(패턴) 추가 : conf 파일 자동으로 문자열 추가 (-i 옵션은 변경후 저장)
logstash.conf
if [idName] =~ "4779" or [idName] =~ "4131" or [idName] =~ "3620" or [idName] =~ "5401" or [idName] =~ "5392" or [idName] =~ "2845" or [idName] =~ "3815" or [idName] =~ "4781" or [idName] =~ "4770" { drop {} } |
Input
#!/bin/bash -x
Before0=`grep policy logstash.conf | awk -F "or" '{print $NF}' | awk -F " " '{print $1, $2, $3}'`
Before='\'${Before0}''
read -p "PolicyID? : " PolicyID
APPEND="$Before or [policyid] =~ $PolicyID"
sed -i "s/${Before}/$APPEND/g" /usr/local/src/logstash.conf
Output (bash shell debug mode)
++ grep policy logstash.conf
++ awk -F or '{print $NF}'
++ awk -F ' ' '{print $1, $2, $3}'
+ Before0='[policyName] =~ "4770"'
+ Before='\[policyName] =~ "4770"'
+ read -p 'PolicyID? : ' PolicyID
PolicyID? : 88888888
+ APPEND='\[policyName] =~ "4770" or [policyid] =~ 88888888'
+ sed -i 's/\[policyName] =~ "4770"/\[policyName] =~ "4770" or [policyid] =~ 88888888/g' /usr/local/src/logstash.conf
3. Line 위, 아래 문자열 추가 : txt 파일 특정 라인 위, 아래 text 추가 하기
file.txt
aaaaaa aaaaaaa bbbbbb bbbbbbb dddddd ddddddd eeeeee eeeeeee gggggg ggggggg |
Input
# 특정 라인(줄) 위에 문자열 추가
sed -i'' -r -e "/dddddd ddddddd/i\cccccc ccccccc" file.txt
# 특정 라인(줄) 아래 문자열 추가
sed -i'' -r -e "/eeeeee eeeeeee/a\ffffff fffffff" file.txt
Output
4. sed 정규표현(regex) 문법 조합 예시 : Regex search & replace
test.txt
TEST Files Telegraf Configuration Telegraf is entirely plugin driven. All metrics are gathered from the declared Inputs, and sent to the Declared outputs. interval = "10s" imetric_batch_size = 1000 servers = ["http://user:pass@localhost:9200"] |
Input
## 대상 파일에서 영어 알파벳 대문자 첫자만 괄호 묶기
cat sample.txt | sed 's/\(\b[A-Z]\)/\(\1\)/g
## 대상 파일에서 모든 숫자를 "#" 으로 치환(바꿈)
cat sample.txt | sed -r 's/[0-9]/#/g'
## 대상 파일의 모든 숫자 끝에 "XXXX"를 추가
cat sample.txt | sed -r 's/([0-9]+)/\1XXXX/g'
Output
5. 파일내 하나 이상의 개행(줄바꿈) 바꾸기 : \n 제거 / 공백으로 교체
in.txt
aaaaaaa bbbbbbb ccccccc ddddddd eeeeeee fffffff ggggggg |
Input
## 파일내 하나 이상의 개행(줄바꿈)을 공백으로 교체
(sed -r ':a;N;$!ba;s/\n+/ /g' | sed -r 's/\s+/ /g') < in.txt > out.txt
Output
6. Overview of Regular Expression Syntax / sed 정규 표현식
https://www.gnu.org/software/sed/manual/html_node/Regular-Expressions.html
Regular Expressions - sed, a stream editor
Matches the null string at beginning of the pattern space, i.e. what appears after the circumflex must appear at the beginning of the pattern space. In most scripts, pattern space is initialized to the content of each line (see How sed works). So, it is a
www.gnu.org
sed 명령어는 다양하고 수많은 옵션과 활용 예제들이 있습니다.
전문서적 책 한권으로 나올정도의 방대한 양의 강력한 리눅스 명령어 입니다.
리눅스 고급 명령어 더보기
'SYSTEM/Linux' 카테고리의 글 목록
정보 지식 공유와 나눔으로 상생의 가치 실현해요 ^^
rootkey.tistory.com