Operating System/Solaris2007/08/03 00:27

17장. Sed

1. sed란?

(1) 개념

  sed는 Stream Editor의 약자로서 File의 수정을 주 목적으로 한다.
  이는 File을 순방향으로 읽는 동안 연산을 수행하며 Text File에서 반복 수정에 용이하다.

(2) 기능

  주어진 Text Pattern을 갖는 모든 행을 delete
특정 행에서 어떤 Text Pattern을 다른 Pattern으로 바꿈
하나의 File을 다른 곳의 File로 복사
  입력 File의 특정 부분을 출력 File로 보냄

2. sed의 수행방법

  sed는 순환적으로 동작하며, 아래와 같은 순서로 실행된다.

(1) 실행순서

① 입력행을 읽어 Pattern 공백으로 옮긴다.

<Pattern 공백? ed와 같이 sed도 편집할 Text를 편집 Buffer에 저장하는데, 이 Buffer를 Pattern 공백이라고 한다.

   ed : File 전체를 Buffer에 저장
sed : 한 행의 Text만 Pattern Buffer에 저장>

  ② 편집 대본(수정할 내용들)을 실행
  ③ Pattern 공백을 출력 값으로 복사

(2) sed 구조

1) sed의 명령행 옵션

Option

명령행

내       용

-n

sed -n

-n Option이 주어지면 Print 명령 주의 하나를 만나야만 출력이 생성된다.

-e

sed -e [command]

-e 다음의 command는 편집 대본이다. 여기서는 여러 개의 편집 대본을 하나의 명랭행에 지정 가능하다.

-f

sed -f [file]

-f 다음에 오는 File은 편집대본을 포함한 File이다.

 2) sed 명령

Option

내   용

S

하나의 문자열을 다른 문자열로 대치한다.

A

다음 입력 행을 읽기 전에 File에 새로운 행을 추가한다.

I

지정한 행에 문자열을 삽입한다.

D

지정한 라인을 삭제한다.

C

주소가 지정된 각 행을 user가 기술한 행으로 바꾼다. (변경(c)명령은 행 전체를 변경하는 것이고, 대치(s)명령은 행의 일부분에 변경, 적용될 수 있다.)

P

출력

W

선택된 라인을 write

3. sed의 기본예제

  <기본 예제>

      % cat remind

      Janet today at 4.
      Call DEC
      add serial line for Brad
      Home at five sharp

  예제 1) 대치(s)명령, 삽입(i)명령, 삭제(d)명령 사용 예제

      <편집대본(File명) : script>

      % cat script

      s/DEC/Dept. Environ. Cons./
      s/today/tomorrow/
      1i\
      Werner and Raquel this weekend\
      Feed polly for adam
      4d

      <결과>

      % sed -f script remind

      Werner and Raquel this weekend
      Feed polly for adam
      Janet tomorrow at 4.
      Call Dept. Environ. Cons.
      add serial line for Brad

  예제 2) 변경(c) 명령 사용 예제

      % cat script1

      2c\
      Dept. Environ. Cons

      <결과>

      % sed -f script1 remind

      Janet today at 4.
      Dept. Environ. Cons
      add serial line for Brad
      Home at five sharp

  예제 3) write명령을 사용한 예제

  % cat script2

  1,2w firstfile
  s/DEC/Dept. Environ. Cons./w rewrite
  4d
  w delfile

  <결 과>

  % sed -f script2 remind

  Janet today at 4.
  Call Dept. Environ. Cons.
  add serial line for Brad

  % cat rewrite

  Call Dept. Environ. Cons.

  % cat firstfile

  Janet today at 4.
  Call DEC

  % cat delfile

  Janet today at 4.
  Call Dept. Environ. Cons.
  add serial line for Brad

  예제 4) g옵션의 사용

  <기본예제>

  % cat file1

  It follows, then, that the divine, being good,
  is not, as most people say
  for the food things in human lige are far fewer than
  the evil, and, whereas the good must be ascribed to heaven only,

  <s Option을 이용하여 문자열 바꾸기>

  % sed -e s/the// -e /for/d file1

  It follows, n, that the divine, being good,
  is not, as most people say
  evil, and, whereas the good must be ascribed to heaven only,

  <g Option을 이용하여 문자열 바꾸기>

  % sed -e s/the//g -e/for/d file1

  It follows, n, that divine, being good,
  is not, as most people say
  evil, and, whereas good must be ascribed to heaven only,

Creative Commons License
Posted by BLUEDAY™