Linux Sed 명령어

아규먼트 부터 Subcommand까지 알아보자

  1. 개념
    1. Subcommand
    2. sed 작동방식
    3. Argument
  2. Reference
    1. sed

개념

sedStream EDitor의 약자입니다. sed는 비대화형 편집기 Sed는 stdin 또는 파일에서 입력을 읽고 stdout으로 보냅니다.

example

$cat sedHelp
# result
If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret.  All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.

GNU sed home page: <https://www.gnu.org/software/sed/>.
General help using GNU software: <https://www.gnu.org/gethelp/>.
E-mail bug reports to: <bug-sed@gnu.org>.

Subcommand

[a[,b]]a\

지정된 라인에 다음 행에 입력된 문자열을 출력합니다

$ sed '1a!!!!!\' sedHelp
If no -e, --expression, -f, or --file option is given, then the first
!!!!!
non-option argument is taken as the sed script to interpret.  All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.

GNU sed home page: <https://www.gnu.org/software/sed/>.
General help using GNU software: <https://www.gnu.org/gethelp/>.
E-mail bug reports to: <bug-sed@gnu.org>.

[a[,b]]c\

지정된 라인을 입력한 문자열로 변환하고 출력합니다. -n 명령과 같이 쓰게된다면 replaceThisWord라는 변환시킨 문자열만 나오게 됩니다.

$ sed '1,3c\replaceThisWord' sedHelp
replaceThieWord
specified, then the standard input is read.

GNU sed home page: <https://www.gnu.org/software/sed/>.
General help using GNU software: <https://www.gnu.org/gethelp/>.
E-mail bug reports to: <bug-sed@gnu.org>.

[a[,b]]d

지정된 라인의 내용을 삭제하고 다음 행으로 스크립트를 재시작합니다.

$ sed '1,3d' sedHelp
specified, then the standard input is read.

GNU sed home page: <https://www.gnu.org/software/sed/>.
General help using GNU software: <https://www.gnu.org/gethelp/>.
E-mail bug reports to: <bug-sed@gnu.org>.

[a[,b]]D

지정된 라인을 포함한 패턴 버퍼만 삭제합니다. 그런 다음 스크립트를 처음부터 다시 시작하고 패턴 버퍼에 남아 있는 텍스트에 적용합니다.

$ sed '1,3D' sedHelp
specified, then the standard input is read.

GNU sed home page: <https://www.gnu.org/software/sed/>.
General help using GNU software: <https://www.gnu.org/gethelp/>.
E-mail bug reports to: <bug-sed@gnu.org>.

[a[,b]]g

홀드 버퍼에서 텍스트 복사본을 잡아 패턴 버퍼에 넣은다음 원본 내용을 덮어씌워 출력합니다.

$sed '1,3g'
1 # 첫 번째 입력

1 # 두 번째 입력

1 # 세 번째 입력

1 # 네 번째 입력 후 버퍼에 집어 넣지 않습니다.
1

[a[,b]]G

홀드 버퍼에서 입력된 텍스트 복사본을 잡고 새줄을 추가한 후 패턴 버퍼 끝에 추가합니다.

$ sed '1,3G'
1 # 첫 번째 입력
1

2 # 두 번째 입력
2

3 # 세 번째 입력
3

4 # 4
4
5 # 5
5
6 # 6
6

[a[,b]]h

텍스트 복사본을 홀드 버퍼에 넣고 원래 내용을 덮어씌워 패턴 버퍼에 저장합니다.

[a[,b]]H

새로운 줄을 추가한 후 홀드 버퍼 끝에 텍스트 복사본을 추가하여 패턴 버퍼에 저장합니다.

[a]i\

텍스트를 삽입합니다.

$ sed '1i\test'
1 # 첫 번째 입력
test
1
2 # 두 번째 입력
2

[a[,b]]l

인쇄할 수 없는 문자(\n,\t,\f )가 보이도록 stdin에 대한 패턴 버퍼를 나열합니다. 행의 끝은 $로 표시됩니다. 문자 \, \a, \b, \f, \r, \t 및 \v는 이스케이프 시퀀스로 인쇄됩니다.

$ sed '1,3l' sedHelp
If no -e, --expression, -f, or --file option is given, then the first$
If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret.  All$
non-option argument is taken as the sed script to interpret.  All
remaining arguments are names of input files; if no input files are$
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.

GNU sed home page: <https://www.gnu.org/software/sed/>.
General help using GNU software: <https://www.gnu.org/gethelp/>.
E-mail bug reports to: <bug-sed@gnu.org>.

sed 작동방식

Argument

-n, --quiet

패턴 스페이스에 있는 내용을 자동으로 출력하지 않습니다.

$ $ sed '1p' sedHelp
If no -e, --expression, -f, or --file option is given, then the first
If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret.  All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read

$ sed -n '1p' sedHelp
If no -e, --expression, -f, or --file option is given, then the first

그냥 사용했을 경우 자동으로 끝까지 나오지마 -n명령어를 이용하면 원하는 행만 나오게 됩니다.

-e script, --expression=script

실행할 스크립트를 추가합니다.

-f script-file, --file=script-file

스크립트 파일을 이용합니다.

--follow-symlinks

제자리에서 작업을 진행할때 심볼릭링크를 따라 갑니다.

-i[SUFFIX], --in-place[=SUFFIX]

변경된 내용을 파일에 적용합니다. SUFFIX가 지정되면 백업 파일을 생성합니다.

-l N, --line-length=N

원하는 행을 지정할 수 있습니다.

--posix

모든 GNU 확장자을 비활성화 합니다.

-E, -r, --regexp-extended

정규표현식을 이용할 수 있습니다.

-s, --separate

파일 하나를 분리하여 별개로 작업을 진행합니다.

--sandbox

안전모드에서 작동합니다

-u, --unbuffered

입력 파일에서 최소량의 데이터를 로드하고 출력 버퍼를 자주 FLUSH 합니다.

-z, --null-data

NULL 문자로 행을 구분합니다.

--help

--version

Reference

sed

IBM sed - Start the sed noninteractive stream editor