# sed를 이용한 문자 치환

> sed는 1970년대 벨 연구소의 리 맥마흔이 처음 만든 유닉스 스트리밍 편집기로서, 정규표현식을 사용하여 텍스트를 변경하는 도구.
> OS : Unix, Linux, Mac

> 옵션
 s : 문자 대체, 예) s/대상문자/대체문자/
 p : 패턴 출력
 q : 프로그램 종료, 행의 마지막에 사용시 오직 첫행만 처리

 ^ : 행의 시작위치
 $ : 행의 마지막위치

 -n 모든 행을 출력
 (-e 또는 ;) : 명령어를 이어 붙이는 역할(다중 명령)
 -f : sed 명령이 파일로 저장된 경우 sed파일 이용
 -i : 텍스트 삽입(실제로 파일의 내용을 바꾸지는 않음)


# 텍스트 파일 출력 - 지정 위치부터 시작, 종료

1) 15번 라인에서 30번째 라인까지 출력
[sunshiny@hostname logs]# sed -n '15,30p' access_log
67.55.215.76 - - [22/Feb/2013:11:54:52 +0900] "GET /robots.txt HTTP/1.1" 404 208
67.55.215.76 - - [22/Feb/2013:11:54:52 +0900] "GET /img/upload/faq.jpg HTTP/1.1" 200 26212
123.234.231.193 - - [22/Feb/2013:11:55:43 +0900] "GET /path/00.html?currentPage=
123.234.231.193 - - [22/Feb/2013:12:24:54 +0900] "GET /path/00.html?currentPage=
123.234.231.193 - - [22/Feb/2013:12:54:51 +0900] "GET /path/00.html?currentPage=
123.234.231.213 - - [22/Feb/2013:13:02:54 +0900] "GET / HTTP/1.1" 200 298
123.234.231.213 - - [22/Feb/2013:13:02:54 +0900] "GET /index.html HTTP/1.1" 200 9647
123.234.231.213 - - [22/Feb/2013:13:02:54 +0900] "GET /css/reset.css HTTP/1.1" 200 1753
123.234.231.213 - - [22/Feb/2013:13:02:54 +0900] "GET /img/path.gif HTTP/1.1" 200 1946
123.234.231.213 - - [22/Feb/2013:13:02:54 +0900] "GET /img/path.gif HTTP/1.1" 200 1568
123.234.231.213 - - [22/Feb/2013:13:02:54 +0900] "GET /img/path.gif HTTP/1.1" 200 1775
123.234.231.213 - - [22/Feb/2013:13:02:54 +0900] "GET /css/common.css HTTP/1.1" 200 15865
123.234.231.213 - - [22/Feb/2013:13:02:54 +0900] "GET /js/jquery-1.7.2.min.js HTTP/1.1" 200 94840
123.234.231.213 - - [22/Feb/2013:13:02:55 +0900] "GET /img/path.gif HTTP/1.1" 200 1847
123.234.231.213 - - [22/Feb/2013:13:02:55 +0900] "GET /img/path.gif HTTP/1.1" 200 5675
123.234.231.213 - - [22/Feb/2013:13:02:55 +0900] "GET /swf/main.swf HTTP/1.1" 200 56641

2) 라인번호를 출력, 15번 라인에서 30번째 라인까지 출력
[sunshiny@hostname logs]# cat -n access_log | sed -n '15,30p'
    15  67.55.215.76 - - [22/Feb/2013:11:54:52 +0900] "GET /robots.txt HTTP/1.1" 404 208
    16  67.55.215.76 - - [22/Feb/2013:11:54:52 +0900] "GET /img/upload/faq.jpg HTTP/1.1" 200 26212
    17  123.234.231.193 - - [22/Feb/2013:11:55:43 +0900] "GET /path/00.html?currentPage=
    18  123.234.231.193 - - [22/Feb/2013:12:24:54 +0900] "GET /path/00.html?currentPage=
    19  123.234.231.193 - - [22/Feb/2013:12:54:51 +0900] "GET /path/00.html?currentPage=
    20  123.234.231.213 - - [22/Feb/2013:13:02:54 +0900] "GET / HTTP/1.1" 200 298
    21  123.234.231.213 - - [22/Feb/2013:13:02:54 +0900] "GET /index.html HTTP/1.1" 200 9647
    22  123.234.231.213 - - [22/Feb/2013:13:02:54 +0900] "GET /css/reset.css HTTP/1.1" 200 1753
    23  123.234.231.213 - - [22/Feb/2013:13:02:54 +0900] "GET /img/path.gif HTTP/1.1" 200 1946
    24  123.234.231.213 - - [22/Feb/2013:13:02:54 +0900] "GET /img/path.gif HTTP/1.1" 200 1568
    25  123.234.231.213 - - [22/Feb/2013:13:02:54 +0900] "GET /img/path.gif HTTP/1.1" 200 1775
    26  123.234.231.213 - - [22/Feb/2013:13:02:54 +0900] "GET /css/common.css HTTP/1.1" 200 15865
    27  123.234.231.213 - - [22/Feb/2013:13:02:54 +0900] "GET /js/jquery-1.7.2.min.js HTTP/1.1" 200 94840
    28  123.234.231.213 - - [22/Feb/2013:13:02:55 +0900] "GET /img/path.gif HTTP/1.1" 200 1847
    29  123.234.231.213 - - [22/Feb/2013:13:02:55 +0900] "GET /img/path.gif HTTP/1.1" 200 5675
    30  123.234.231.213 - - [22/Feb/2013:13:02:55 +0900] "GET /swf/main.swf HTTP/1.1" 200 56641

# 문자열 치환
[sunshiny@hostname logs]# echo Hello | sed s/Hello/HelloSunshiny/
HelloSunshiny

# 행의 시작위치에 <h1> 을, 마지막 위치에 </h1> 와 같이 마크업 추가

1) ; 옵션을 사용하여 다중 명령 처리
[sunshiny@hostname logs]# sed -n 's/^/<h1>/;s/$/<\/h1>/p;q' access_log
<h1>123.234.231.213 - - [21/Feb/2013:20:24:43 +0900] "GET / HTTP/1.1" 200 45</h1>

2) -e 옵션을 사용하여 다중 명령 처리
[sunshiny@hostname logs]# sed -ne 's/^/<h1>/' -e 's/$/<\/h1>/p' -e 'q' access_log
<h1>123.234.231.213 - - [21/Feb/2013:20:24:43 +0900] "GET / HTTP/1.1" 200 45</h1>

> 파일로 저장
[sunshiny@hostname logs]# cat h1.sed
#!/usr/bin/sed

s/^/<h1>/
s/$/<\/h1>/
q

[sunshiny@hostname logs]# sed -f h1.sed access_log
<h1>123.234.231.213 - - [21/Feb/2013:20:24:43 +0900] "GET / HTTP/1.1" 200 45</h1>

# 텍스트 삽입

> sed_test.txt 파일 시작위치에 Html 추가 테스트
[sunshiny@hostname logs]# cat sed_test.txt
Sed  is  a  stream  editor.  A stream editor is used to perform basic text transformations on an input
       stream (a file or input from a pipeline).

[sunshiny@hostname logs]# sed '1 i\
> <!DOCTYPE html>\
> <html lang="en">\
> <head>\
> <title>Html Tab Insert</title>\
> <meta charset="utf-8"/>\
> </head>\
> <body>\
> q' sed_test.txt
------------ 출력 ------------------
<!DOCTYPE html>
<html lang="en">
<head>
<title>Html Tab Insert</title>
<meta charset="utf-8"/>
</head>
<body>
q
Sed  is  a  stream  editor.  A stream editor is used to perform basic text transformations on an input
       stream (a file or input from a pipeline).
------------------------------------

# 텍스트 내용 대체

- 대체 명령(s)은 첫번째 행을 참조 그룹(^(.*)$)으로 지정, 이 내용을 \1로 다시 참조
1) <title> 앞에 공백 추가

[sunshiny@hostname logs]# sed '1s/^\(.*\)$/ <title>\1<\/title>/;q' sed_test.txt
 <title>Sed  is  a  stream  editor.  A stream editor is used to perform basic text transformations on an input</title>

2)
[sunshiny@hostname logs]# sed '1s/^\(.*\)$/<!DOCTYPE html>\
> <html lang="en">\
> <head>\
>  <title>\1<\/title>\
> <\/head>\
> <body>\
>  <h1>\1<\/h1>\
> /;q' sed_test.txt
<!DOCTYPE html>
<html lang="en">
<head>
 <title>Sed  is  a  stream  editor.  A stream editor is used to perform basic text transformations on an input</title>
</head>
<body>
 <h1>Sed  is  a  stream  editor.  A stream editor is used to perform basic text transformations on an input</h1>

# 로마 숫자 처리

sed -En 's/^(ARGUMENT\.|I{0,3}V?I{0,2}\.)$/<h2>\1<\/h2>/p' sed_test.txt

# 문단 처리

sed -En '5s/^([A-Z].*)$/<p>\1<\/p>/p' sed_test.txt

# 상단, 하단에 공백 추가

[sunshiny@hostname logs]# cat sed_test.txt
Sed is a stream editor.
A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).
While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.
But it is sed’s ability to filter text in a pipeline which particularly distinguishes it from other types of editors.
[sunshiny@hostname logs]#

> 상단에 공백 추가
[sunshiny@hostname logs]# sed '/editor/{x;p;x;}' sed_test.txt

Sed is a stream editor.

A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).

While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.

But it is sed’s ability to filter text in a pipeline which particularly distinguishes it from other types of editors

> 하단에 공백 추가
[sunshiny@hostname logs]# sed '/editor/G' sed_test.txt
Sed is a stream editor.

A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).

While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.

But it is sed’s ability to filter text in a pipeline which particularly distinguishes it from other types of editors.

[sunshiny@hostname logs]#

# 개행후 공백이 있는 곳에 대체 문자(<br/>) 넣기

[sunshiny@hostname logs]# sed 's/^$/<br\/>/' sed_test.txt
Sed is a stream editor.
<br/>
A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).
While in some ways similar to an editor which permits scripted edits (such as ed),
<br/>
sed works by making only one pass over the input(s),
and is consequently more efficient.
<br/>
But it is sed’s ability to filter text in a pipeline which particularly distinguishes it from other types of editors.

# 기타 참고 자료

http://sed.sourceforge.net/grabbag/tutorials/











※ 위 내용은, 여러 자료를 참고하거나 제가 주관적으로 정리한 것입니다.
   잘못된 정보나 보완이 필요한 부분을, 댓글 또는 메일로 보내주시면 많은 도움이 되겠습니다.
07 23, 2013 18:16 07 23, 2013 18:16


Trackback URL : http://develop.sunshiny.co.kr/trackback/936

Leave a comment

« Previous : 1 : ... 93 : 94 : 95 : 96 : 97 : 98 : 99 : 100 : 101 : ... 648 : Next »

Recent Posts

  1. HDFS - Python Encoding 오류 처리
  2. HP - Vertica ROS Container 관련 오류...
  3. HDFS - Hive 실행시 System Time 오류
  4. HP - Vertica 사용자 쿼리 이력 테이블...
  5. Client에서 HDFS 환경의 데이터 처리시...

Recent Comments

  1. 안녕하세요^^ 배그핵
  2. 안녕하세요^^ 도움이 되셨다니, 저... sunshiny
  3. 정말 큰 도움이 되었습니다.. 감사합... 사랑은
  4. 네, 안녕하세요. 댓글 남겨 주셔서... sunshiny
  5. 감사합니다 많은 도움 되었습니다!ㅎㅎ 프리시퀸스

Recent Trackbacks

  1. how to broadcast your own tv station how to broadcast your own tv station %M
  2. elapsed time clock for operating r... elapsed time clock for operating r... %M
  3. Mysql - mysql 설치후 Character set... 멀고 가까움이 다르기 때문 %M

Calendar

«   12 2019   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        

Bookmarks

  1. 위키피디아
  2. MysqlKorea
  3. 오라클 클럽
  4. API - Java
  5. Apache Hadoop API
  6. Apache Software Foundation
  7. HDFS 생태계 솔루션
  8. DNSBL - Spam Database Lookup
  9. Ready System
  10. Solaris Freeware
  11. Linux-Site
  12. 윈디하나의 솔라나라

Site Stats

TOTAL 2780494 HIT
TODAY 77 HIT
YESTERDAY 1360 HIT