"Sed"의 두 판 사이의 차이
ph
잔글 |
잔글 |
||
9번째 줄: | 9번째 줄: | ||
*다음 유형들이 모두 유효하다<blockquote># sed -n ‘N’p filename<br># sed -n ‘M~N’p filename<br># sed -n ‘M,N’p filename<br># sed -n ‘$’p filename<br># sed -n ‘N,$p’ filename</blockquote> | *다음 유형들이 모두 유효하다<blockquote># sed -n ‘N’p filename<br># sed -n ‘M~N’p filename<br># sed -n ‘M,N’p filename<br># sed -n ‘$’p filename<br># sed -n ‘N,$p’ filename</blockquote> | ||
** 'N'p나 'M,N'p대신 'Np'나 'M,Np'를 써도 된다. 따옴표를 모두 제거하고 Np나 M,Np로 써도 된다. 하지만 'N,$'등과 같이 기호가 들어가면 꼭 따옴표 안에 넣어주어야 한다. | ** 'N'p나 'M,N'p대신 'Np'나 'M,Np'를 써도 된다. 따옴표를 모두 제거하고 Np나 M,Np로 써도 된다. 하지만 'N,$'등과 같이 기호가 들어가면 꼭 따옴표 안에 넣어주어야 한다. | ||
− | ** BSD버전 sed는 '~'가 | + | ** BSD버전 sed는 '~'가 듣지 않았다.(mac os x) |
+ | ** $를 pattern으로 지정하면 맨 끝 줄에 명령이 적용된다. | ||
*다음 유형들도 모두 유효하다<blockquote># sed -n /PATTERN/p filename<br># sed -n ‘/PATTERN/,Np’ filename<br># sed -n ‘N,/PATTERN/p’ filename<br># sed -n ‘/PATTERN/,$p’ filename<br># sed -n ‘/PATTERN/,+Np’ filename<br># sed -n ‘/P1/,/P2/p’ filename</blockquote> | *다음 유형들도 모두 유효하다<blockquote># sed -n /PATTERN/p filename<br># sed -n ‘/PATTERN/,Np’ filename<br># sed -n ‘N,/PATTERN/p’ filename<br># sed -n ‘/PATTERN/,$p’ filename<br># sed -n ‘/PATTERN/,+Np’ filename<br># sed -n ‘/P1/,/P2/p’ filename</blockquote> | ||
+ | == | ||
==pattern space, hold space== | ==pattern space, hold space== | ||
([http://www.thegeekstuff.com/2009/12/unix-sed-tutorial-7-examples-for-sed-hold-and-pattern-buffer-operations/ 출처]) | ([http://www.thegeekstuff.com/2009/12/unix-sed-tutorial-7-examples-for-sed-hold-and-pattern-buffer-operations/ 출처]) |
2011년 6월 6일 (월) 23:53 판
articles
The Geek Stuff의 unix sed tutorial 예제들
주소지정
(출처)
- 대표유형
# sed -n 'ADDRESS'p filename
# sed -n '/PATTERN/p' filename - 다음 유형들이 모두 유효하다
# sed -n ‘N’p filename
# sed -n ‘M~N’p filename
# sed -n ‘M,N’p filename
# sed -n ‘$’p filename
# sed -n ‘N,$p’ filename- 'N'p나 'M,N'p대신 'Np'나 'M,Np'를 써도 된다. 따옴표를 모두 제거하고 Np나 M,Np로 써도 된다. 하지만 'N,$'등과 같이 기호가 들어가면 꼭 따옴표 안에 넣어주어야 한다.
- BSD버전 sed는 '~'가 듣지 않았다.(mac os x)
- $를 pattern으로 지정하면 맨 끝 줄에 명령이 적용된다.
- 다음 유형들도 모두 유효하다
# sed -n /PATTERN/p filename
# sed -n ‘/PATTERN/,Np’ filename
# sed -n ‘N,/PATTERN/p’ filename
# sed -n ‘/PATTERN/,$p’ filename
# sed -n ‘/PATTERN/,+Np’ filename
# sed -n ‘/P1/,/P2/p’ filename
==
pattern space, hold 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옵션이 없음에 다시 유의)
- 빈 줄이 나올 때까지 모두 hold space로 옮긴다. $!d; d명령은 Delete the pattern space and start the next cycle. 이므로, 이 명령이 없으면 다음 cycle을 시작하기 전 모두 출력되고(sed명령에 -n옵션이 없으므로) hold space는 비워져버린다.
- 특정 패턴이 등장하는 줄의 바로 직전 줄을 출력하기
$ sed -n '/Mysql/{g;1!p;};h' thegeekstuff.txt
- 모든 줄에 대해 hold space로 옮기는 h명령이 적용된다
- 만일 패턴(이 명령에서는 Mysql)이 일치하면, hold space에 있던 라인(직전 라인)을 가져와서 출력.
- 만일 첫번째 줄에서 일치하는 것이 나온다면, hold space는 비어 있을 것이므로 그 라인을 출력하지 않기 위해 p가 아니라 1!p를 줌. sed가 각 라인별로 실행되지만 현재 라인수를 기억하고 있음을 주시할 것.
- 각 문단의 마지막줄을 삭제하기
$ sed -n -e '/^$/{x;d;}' -e '/./x;p' thegeekstuff.txt
- 빈 줄이 아니면 pattern space와 hold space를 exchange하고 출력한다. 첫줄에 hold space는 빈줄이므로 빈 줄이 출력될 것이다. 이를 막기 위해 1!p를 줄 수 있다.
- 빈 줄이 나왔을 때 {x;d;}가 실행되는데, hold space에는 직전 줄이 들어있으므로, exchange후 d명령 실행으로 삭제된다.
- 각 줄 끝에 직전 줄을 붙이기
$ sed 'H;x;s/^\(.*\)\n\(.*\)/\2\1/' thegeekstuff.txt
- 모든 줄의 처음부분에 특정 단어를 넣되, 단락별로 다르게. 각 단락은 '#'으로 시작하는 특정 단어(=tag)를 가짐.(Prepend tag of every block to every line of that block)
$ sed '
/^#/{
h
d
}
G
s/^\(.*\)\n#\(.*\)/\2 \1/' thegeekstuff.txt- '#'으로 시작하는 줄을 만나면 hold space로 옮기고 pattern space를 비운 뒤, (출력하지 않고) 다음 cycle진행.
- (모든 줄에 대해) hold space에 있는 것을 가져와서 pattern space에 붙이고, 치환명령으로 그 둘의 자리를 바꿈.(tag이 행 처음에 나오도록.)