쉘스크립트
7. sed 란?
Dorori
2009. 3. 31. 17:01
첫번째 라인 출력
sed -n '1p' filename
마지막 라인 출력
sed -n '$p' filename
두번째 라인 출력
sed -n '2p' filename
첫번째부터 세번째 라인까지 출력 ( 1번 라인 부터 3번 라인까지 포함 )
sed -n '1,3p' filename
첫번째라인 부터 세번째 라인까지 삭제하고 출력
sed '1,3d' filename
특수한 문자열(예를들어 root)을 검색해서 그 라인만 출력
sed -n '/root/p' filename
love가 나오는 행과 peace가 나오는 행 사이의 모든 행들이 출력된다. love가 peace 다음에 나오면 love가 나오는 행부터 마지막 행까지 출력된다.
sed -n ‘/love/,/peace/p’ datafile
파일 안의 빈줄들만 모조리 삭제하고자 할때 .
sed '/^$d' 파일명 > 새파일명
특정 단어를 찾아서 변환하고자 할때
sed 's/TEST/test/g' > 새파일명
특정 단어를 찾아서 지우고자 할때
sed 's/TEST//g' > 새파일명
sed 에 관한 도움말
# sed --help
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...
suppress automatic printing of pattern space
-e script, --expression=script
add the script to the commands to be executed
-f script-file, --file=script-file
add the contents of script-file to the commands to be executed
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if extension supplied)
-c, --copy
use copy instead of rename when shuffling files in -i mode
(avoids change of input file ownership)
-l N, --line-length=N
specify the desired line-wrap length for the `l' command
--posix
disable all GNU extensions.
-r, --regexp-extended
use extended regular expressions in the script.
-s, --separate
consider files as separate rather than as a single continuous
long stream.
-u, --unbuffered
load minimal amounts of data from the input files and flush
the output buffers more often
--help display this help and exit
--version output version information and exit