"Sed"의 두 판 사이의 차이

ph
이동: 둘러보기, 검색
잔글
1번째 줄: 1번째 줄:
 
[http://wiki.kldp.org/Translations/html/Sed-KLDP/index.html SED 스트림 에디터]
 
[http://wiki.kldp.org/Translations/html/Sed-KLDP/index.html SED 스트림 에디터]
 
* hold, pattern space에 관한 예들. ([http://www.thegeekstuff.com/2009/12/unix-sed-tutorial-7-examples-for-sed-hold-and-pattern-buffer-operations/ 출처])
 
* hold, pattern space에 관한 예들. ([http://www.thegeekstuff.com/2009/12/unix-sed-tutorial-7-examples-for-sed-hold-and-pattern-buffer-operations/ 출처])
** file.txt의 내용을 double space로 <blockquote>$ sed 'G' thegeekstuff.txt</blockquote>
+
** thegeekstuff.txt의 내용을 double space로 <blockquote>$ sed 'G' thegeekstuff.txt</blockquote>
 
*** 한 줄 읽어 pattern space로 읽어오면,
 
*** 한 줄 읽어 pattern space로 읽어오면,
 
*** G명령으로 hold space에 있는 것이 가서 붙음.(Append a newline character followed by the contents of the hold space to the pattern space.이므로 hold space가 비었다면 빈 줄이 붙는다)
 
*** G명령으로 hold space에 있는 것이 가서 붙음.(Append a newline character followed by the contents of the hold space to the pattern space.이므로 hold space가 비었다면 빈 줄이 붙는다)
 
*** 같은 방식으로 'G;G'하면 triple space가 됨.
 
*** 같은 방식으로 'G;G'하면 triple space가 됨.
** file.txt를 줄(line)단위로 역순 출력. <blockquote>$ sed -n '1!G;h;$p' thegeekstuff.txt</blockquote>
+
** thegeekstuff.txt를 줄(line)단위로 역순 출력. <blockquote>$ sed -n '1!G;h;$p' thegeekstuff.txt</blockquote>
 
*** 한줄을 읽어온 뒤 그대로 hold space로 저장한다
 
*** 한줄을 읽어온 뒤 그대로 hold space로 저장한다
 
*** 그 줄을 제외하고(2줄부터) G;h 가 반복되는데,
 
*** 그 줄을 제외하고(2줄부터) G;h 가 반복되는데,
11번째 줄: 11번째 줄:
 
*** 다음 h명령이 pattern space를 hold space로 옮긴다
 
*** 다음 h명령이 pattern space를 hold space로 옮긴다
 
*** 마지막까지 반복되고 파일 끝($)에서 출력된다.
 
*** 마지막까지 반복되고 파일 끝($)에서 출력된다.
 +
** 특정 패턴이 포함된 문단만 출력 <blockquote>$ sed -e '/./{H;$!d;}' -e 'x;/Administration/!d' thegeekstuff.txt </blockquote>
 +
*** 빈 줄이 나올 때까지 모두 hold space로 옮긴다. $!d; d명령은 Delete the pattern space and start the next cycle. 이므로, 이 명령이 없으면 다음 cycle을 시작하기 전 모두 출력되고(sed명령에 -n옵션이 없으므로) hold space는 비워져버린다.
 +
**** ! (exclamation) :  Editing commands can be applied to <u>non-selected pattern spaces</u> by use of the exclamation character (``!'') function.
 +
**** 위 명령에서 .(dot)은 모든 character에 해당하므로 $!d명령은 start the next cycle의 효과만 가지게 된다.
 +
*** 빈 줄이 나오면 hold space와 pattern space를 exchange. 주어진 패턴이 없다면 지운다. 있으면 출력되게 된다.(sed에 -n옵션이 없음에 다시 유의)

2011년 6월 6일 (월) 22:09 판

SED 스트림 에디터

  • hold, pattern space에 관한 예들. (출처)
    • thegeekstuff.txt의 내용을 double space로

      $ sed 'G' thegeekstuff.txt

      • 한 줄 읽어 pattern space로 읽어오면,
      • G명령으로 hold space에 있는 것이 가서 붙음.(Append a newline character followed by the contents of the hold space to the pattern space.이므로 hold space가 비었다면 빈 줄이 붙는다)
      • 같은 방식으로 'G;G'하면 triple space가 됨.
    • thegeekstuff.txt를 줄(line)단위로 역순 출력.

      $ sed -n '1!G;h;$p' thegeekstuff.txt

      • 한줄을 읽어온 뒤 그대로 hold space로 저장한다
      • 그 줄을 제외하고(2줄부터) G;h 가 반복되는데,
      • G명령으로 hold space에 있는 것들이 pattern space에 붙는다. 1줄이 그대로 hold space에 들어 있으므로 (2줄이 pattern space에 들어온 뒤) 1줄이 2줄 뒤에 붙게 된다.
      • 다음 h명령이 pattern space를 hold space로 옮긴다
      • 마지막까지 반복되고 파일 끝($)에서 출력된다.
    • 특정 패턴이 포함된 문단만 출력

      $ sed -e '/./{H;$!d;}' -e 'x;/Administration/!d' thegeekstuff.txt

      • 빈 줄이 나올 때까지 모두 hold space로 옮긴다. $!d; d명령은 Delete the pattern space and start the next cycle. 이므로, 이 명령이 없으면 다음 cycle을 시작하기 전 모두 출력되고(sed명령에 -n옵션이 없으므로) hold space는 비워져버린다.
        •  ! (exclamation) : Editing commands can be applied to non-selected pattern spaces by use of the exclamation character (``!) function.
        • 위 명령에서 .(dot)은 모든 character에 해당하므로 $!d명령은 start the next cycle의 효과만 가지게 된다.
      • 빈 줄이 나오면 hold space와 pattern space를 exchange. 주어진 패턴이 없다면 지운다. 있으면 출력되게 된다.(sed에 -n옵션이 없음에 다시 유의)