"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/ 출처])
 +
** file.txt의 내용을 double space로 <blockquote>$ sed 'G' thegeekstuff.txt</blockquote>
 +
*** 한 줄 읽어 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가 됨.
 +
** file.txt를 줄(line)단위로 역순 출력. <blockquote>$ sed -n '1!G;h;$p' thegeekstuff.txt</blockquote>
 +
*** 한줄을 읽어온 뒤 그대로 hold space로 저장한다
 +
*** 그 줄을 제외하고(2줄부터) G;h 가 반복되는데,
 +
*** G명령으로 hold space에 있는 것들이 pattern space에 붙는다. 1줄이 그대로 hold space에 들어 있으므로 (2줄이 pattern space에 들어온 뒤) 1줄이 2줄 뒤에 붙게 된다.
 +
*** 다음 h명령이 pattern space를 hold space로 옮긴다
 +
*** 마지막까지 반복되고 파일 끝($)에서 출력된다.

2011년 6월 6일 (월) 21:48 판

SED 스트림 에디터

  • hold, pattern space에 관한 예들. (출처)
    • file.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가 됨.
    • file.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로 옮긴다
      • 마지막까지 반복되고 파일 끝($)에서 출력된다.