Sed (유틸리티): 두 판 사이의 차이

위키백과, 우리 모두의 백과사전.
내용 삭제됨 내용 추가됨
편집 요약 없음
TedBot (토론 | 기여)
잔글 봇: 틀 이름 및 스타일 정리
9번째 줄: 9번째 줄:
| quote = "A while later a demand arose for another special-purpose program, gres, for substitution: g/re/s. Lee McMahon undertook to write it, and soon foresaw that there would be no end to the family: g/re/d, g/re/a, etc. As his concept developed it became sed…"
| quote = "A while later a demand arose for another special-purpose program, gres, for substitution: g/re/s. Lee McMahon undertook to write it, and soon foresaw that there would be no end to the family: g/re/d, g/re/a, etc. As his concept developed it became sed…"
}}
}}
</ref> 원래는 치환을 목적으로 한 grep(g/re/p)의 상이형인 "g/re/s"이었다.<ref name="reader">{{cite techreport |first1=M. D. |last1=McIlroy |authorlink1=Doug McIlroy |year=1987 |url=http://www.cs.dartmouth.edu/~doug/reader.pdf |title=A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971–1986 |series=CSTR |number=139 |institution=Bell Labs}}</ref> 개별 명령어를 위해 추가적인 특수 목적의 프로그램들이 등장할 것으로 예견하면서 맥마흔은 범용 목적의 라인 지향 스트림 편집기를 작성하였으며, 이것이 sed로 되었다.<ref name=early_history />
</ref> 원래는 치환을 목적으로 한 grep(g/re/p)의 상이형인 "g/re/s"이었다.<ref name="reader">{{기술보고서 인용|first1=M. D. |last1=McIlroy |authorlink1=Doug McIlroy |year=1987 |url=http://www.cs.dartmouth.edu/~doug/reader.pdf |title=A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971–1986 |series=CSTR |number=139 |institution=Bell Labs}}</ref> 개별 명령어를 위해 추가적인 특수 목적의 프로그램들이 등장할 것으로 예견하면서 맥마흔은 범용 목적의 라인 지향 스트림 편집기를 작성하였으며, 이것이 sed로 되었다.<ref name=early_history />


== 사용법 ==
== 사용법 ==
30번째 줄: 30번째 줄:
</source>
</source>
즉, "generateData"와 같은 프로그램은 데이터를 만든 다음 x를 y로 대체하는 사소한 변경을 취한다.
즉, "generateData"와 같은 프로그램은 데이터를 만든 다음 x를 y로 대체하는 사소한 변경을 취한다.
* 예 :
* 예 :
<source lang=console>
<source lang=console>
$ echo xyz xyz | sed 's/x/y/g'
$ echo xyz xyz | sed 's/x/y/g'

2017년 10월 18일 (수) 21:54 판

sed(stream editor)는 유닉스에서 텍스트를 분해하거나 변환하기 위한 프로그램이다. sed는 벨 연구소리 E. 맥마흔이 1973년부터 1974년까지 개발하였고, 현재 유닉스 등의 여러가지 운영 체제에서 사용 가능하다.

역사

버전 7 유닉스에서 처음 등장한[1] sed는 데이터 파일의 명령 줄 처리를 위해 개발된 초기 유닉스 명령어들 가운데 하나이다. 대중적인 grep 명령어의 뒤를 자연스럽게 이을 정도로 발전하였다.[2] 원래는 치환을 목적으로 한 grep(g/re/p)의 상이형인 "g/re/s"이었다.[1] 개별 명령어를 위해 추가적인 특수 목적의 프로그램들이 등장할 것으로 예견하면서 맥마흔은 범용 목적의 라인 지향 스트림 편집기를 작성하였으며, 이것이 sed로 되었다.[2]

사용법

치환 명령어

다음의 예는 sed의 가장 일반적인 치환의 예이다. 이 사용법은 실제로 sed의 원래 동기와 부합한다:[2]

sed 's/regexp/replacement/g' inputFileName > outputFileName

기타 sed 명령어

치환 외에도 25개의 sed 명령을 사용하여 다른 형태의 단순한 처리가 가능하다. 이를테면, 다음의 경우 d 명령어를 사용하여 비어있거나 공백만 포함하는 줄을 삭제한다:

sed '/^ *$/d' inputFileName

필터로서의 사용

유닉스에서 sed는 파이프 안에 필터로 종종 사용된다:

generateData | sed 's/x/y/g'

즉, "generateData"와 같은 프로그램은 데이터를 만든 다음 x를 y로 대체하는 사소한 변경을 취한다.

  • 예 :
$ echo xyz xyz | sed 's/x/y/g'
yyz yyz

파일 기반 sed 스크립트

한 줄에 하나의 명령으로 여러 sed 명령을 subst.sed와 같은 스크립트 파일 안에 넣으면 유용할 수 있으며 -f 옵션을 사용하면 파일로부터 s/x/y/g와 같은 명령을 실행할 수 있다.

sed -f subst.sed inputFileName > outputFileName

수정 편집

GNU sed에 도입된 -i 옵션을 사용하면 파일의 직접 수정을 가능케 한다. 이를테면 다음과 같다:

sed -i 's/abc/def/' fileName

같이 보기

각주

  1. McIlroy, M. D. (1987). 《A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971–1986》 (PDF) (기술 보고서). CSTR. Bell Labs. 139. 
  2. “On the Early History and Impact of Unix”. A while later a demand arose for another special-purpose program, gres, for substitution: g/re/s. Lee McMahon undertook to write it, and soon foresaw that there would be no end to the family: g/re/d, g/re/a, etc. As his concept developed it became sed… 

외부 링크