Linux
기본명령어
sed
#
Find similar titles
- 최초 작성자
-
최근 업데이트
nwchoi@insilicogen.com
Structured data
- Category
- Computer science
sed #
NAME #
sed – 필터링과 텍스트를 변환하는 스트림 편집기. 원본 변화없이, 출력 결과를 변화
Syntex #
- sed 's/찾을텍스트/바꿀텍스트/' 파일명
Option #
- -n, --quiet, --silent
- 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
- --follow-symlinks
- follow symlinks when processing in place; hard links will still be broken.
- -i[SUFFIX], --in-place[=SUFFIX]
- edit files in place (makes backup if extension supplied). The default operation mode is to break symbolic and hard links. This can be changed with --follow-symlinks and --copy.
- -c, --copy
- use copy instead of rename when shuffling files in -i mode. While this will avoid breaking links (symbolic or hard), the resulting editing operation is not atomic. This is rarely the desired mode; --follow-symlinks is usually enough, and it is both faster and more secure.
- -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
정규식 #
- []: 한문자 매치
- ^: 라인 맨앞
- $: 라인 끝
- &: 검색문자열 치환
- \<: 단어의 시작
- >: 단어의 끝
- x{m,n}: m,n 구간 반복
연산자 #
- [범위]/p: 지정 범위 출력
- [범위]/d: 지정 범위 삭제
- [범위]/문자1/문자2: 지정된 범위의 처음 나타난 문자를 문자2로
- s/문자1/문자2/: 문자1을 2로
- g: 모든 라인에 적용
예제 #
- 특정라인 출력
- sed -n '3,7p' <파일> # <파일>에서 3~7라인만 출력
- 특정라인 제외 출력
- sed '3,7d' <파일> # 3~7라인 제외하고 출력
- 특정라인(비연속) 출력
- sed -n -e '3,5p' -e '7,9p' <파일> # 3~5, 3~7라인만 출력
- 치환하여 출력
- sed 's/1/2/g' <파일> # 1을 2로 바꿔서 출력
- sed 's/1/2/gi' <파일> # 1을 2로 바꿔서 출력(대소문자 무시)
- 특정라인에서 치환
- sed '2,5 s/4/7/g' <파일> # <파일>의 2~5줄내에서 4를 7로 치환 출력
- 정규식(예제1)
- sed '/^#\|^$\| *#/d' <파일> # "#"로 시작하거나 or 빈 줄인 라인 제거
- 정규식(예제2)
- sed 's/[Zz]ip/rar/g' <파일> # zip 또는 Zip을 rar로 바꾸기
- 패턴 출력
- sed -n '/^May 22/ p' <파일> # "May 22" 로 시작하는 라인 출력
- 공백 라인 넣기
- sed G <파일> # 라인마다 공백라인 1줄 추가
- sed 'G;G' <파일> # 라인마다 공백라인 2줄 추가
- 에뮬레이팅 dos2unix
- sed -i 's/\r//' <파일> # hidden new line 제거
- In-place 편집 및 백업 (-i 옵션)
- sed -i'.org' 's/this/that/gi' <파일> # 원본 수정 후 .org 확장자 파일 생성하여 백업
- 쌍단어 스위칭
- sed 's/^\(.*\),\(.*\)$/\2\, \1/g' <파일> # 특정 구분자(comma)를 기준으로 앞뒤 바꿈
- 특정 조건내 변환
- sed '/services/ s/start/stop/g' <파일> # services를 갖는 줄에서 start를 stop으로 global하게 변환
- 두개 이상 동시 변환
- sed -i 's/that/this/gi;s/line/verse/gi' <파일> # that을 this로 / line을 verse로 동시에 바꿈
- 다른 명령어와 동시 사용
- ip route show | sed -n '/src/p' | sed -e 's/ */ /g' | cut -d' ' -f9 # "|"를 이용하여 최종적으로 ip 추출("src"를 갖는 줄에서 연속된 공백을 하나로 치환하고 9번째 컬럼 값인 ip를 출력)
참조 #
- 더 자세한 내용이 필요한 경우 Texinfo 매뉴얼을 참조
- info sed
- https://www.tecmint.com/linux-sed-command-tips-tricks/