"Sed"의 두 판 사이의 차이

ph
이동: 둘러보기, 검색
잔글
잔글
15번째 줄: 15번째 줄:
 
**** ! (exclamation) :  Editing commands can be applied to <u>non-selected pattern spaces</u> by use of the exclamation character function.  
 
**** ! (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의 효과만 가지게 된다.
 
**** 위 명령에서 .(dot)은 모든 character에 해당하므로 $!d명령은 start the next cycle의 효과만 가지게 된다.
*** 빈 줄이 나오면 hold space와 pattern space를 exchange. 주어진 패턴이 없다면 지운다. 있으면 출력되게 된다.(sed에 -n옵션이 없음에 다시 유의)
+
*** 빈 줄이 나오면 hold space와 pattern space를 exchange. 주어진 패턴이 없다면 지운다. 있으면 출력된다.(sed에 -n옵션이 없음에 다시 유의)
 +
** 특정 패턴이 등장하는 줄의 바로 직전 줄을 출력하기 <blockqoute>$ sed -n '/Mysql/{g;1!p;};h' thegeekstuff.txt</blockquote>
 +
*** 모든 줄에 대해 hold space로 옮기는 h명령이 적용된다
 +
*** 만일 패턴(이 명령에서는 Mysql)이 일치하면, hold space에 있던 라인(직전 라인)을 가져와서 출력.
 +
*** 만일 첫번째 줄에서 일치하는 것이 나온다면, hold space는 비어 있을 것이므로 그 라인을 출력하지 않기 위해 p가 아니라 1!p를 줌. sed가 각 라인별로 실행되지만 현재 라인수를 기억하고 있음을 주시할 것.

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

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옵션이 없음에 다시 유의)
    • 특정 패턴이 등장하는 줄의 바로 직전 줄을 출력하기 <blockqoute>$ sed -n '/Mysql/{g;1!p;};h' thegeekstuff.txt
      • 모든 줄에 대해 hold space로 옮기는 h명령이 적용된다
      • 만일 패턴(이 명령에서는 Mysql)이 일치하면, hold space에 있던 라인(직전 라인)을 가져와서 출력.
      • 만일 첫번째 줄에서 일치하는 것이 나온다면, hold space는 비어 있을 것이므로 그 라인을 출력하지 않기 위해 p가 아니라 1!p를 줌. sed가 각 라인별로 실행되지만 현재 라인수를 기억하고 있음을 주시할 것.