sed 명령어 (AIX 5.3·6.1)
https://archive.horanglab.com/posts/aix-cmd-sed
핵심 정리
- 편집 스크립트에 따라 파일의 줄을 고쳐 표준 출력에 쓰는 스트림 편집기입니다. 원본 파일은 바꾸지 않습니다.
- 줄을 패턴 공간에 읽어 주소가 맞는 하위 명령을 차례로 적용하고 출력합니다. 보관 공간(hold space)에 내용을 잠시 둘 수 있습니다.
- 주소는 줄 번호,
$(마지막 줄),/패턴/이고,주소1,주소2로 범위를 지정합니다.!를 붙이면 맞지 않는 줄에 적용합니다. - 자주 쓰는 하위 명령:
s/패턴/바꿀말/g(바꾸기),d(지우기),p(출력),y/abc/xyz/(문자 바꾸기),a\·i\·c\(뒤에 넣기·앞에 넣기·바꾸기),N·D·P,h·g·x(보관 공간). -n은 자동 출력을 끄고p로 고른 줄만 보이게 합니다.-e로 스크립트를 여러 개,-f로 스크립트 파일을 줍니다.-u는 버퍼 없이 출력합니다.- AIX sed에는 GNU의
-i(파일 직접 수정)가 없습니다. 결과를 다른 파일로 받은 뒤 옮깁니다.
| 항목 | 내용 |
|---|---|
| 버전 | AIX 5.3, AIX 6.1 (내용 거의 같음, # 주석 설명만 다름) |
| 원문 | IBM Commands Reference — 5.3 Volume 5, 6.1 Volume 5 |
| 파일 | /usr/bin/sed |
| 관련 명령어 | ed, awk, grep |
목적
스트림 편집기입니다.
영어 원문
A stream editor.
구문
sed [ -n ] [ -u ] Script [ File ... ]
sed [ -n ] [ -u ] [ -e Script ] ... [ -f ScriptFile ] ... [ File ... ]
설명
sed 명령은 지정한 File 매개변수의 줄을 편집 스크립트에 따라 고쳐 표준 출력에 씁니다. 고칠 줄을 고르고 고른 줄만 바꾸는 기능이 많습니다.
sed는 고치는 줄을 담는 작업 공간을 둘 씁니다. 고른 줄을 담는 패턴 공간(pattern space)과, 줄을 잠시 저장해 둘 수 있는 보관 공간(hold space)입니다.
편집 스크립트는 한 줄에 하나씩 쓴 하위 명령으로 이뤄집니다. 하위 명령의 일반 형태는 다음과 같습니다.
[address-range] function[modifiers]
sed는 입력 파일마다 다음을 되풀이합니다. 입력 줄 하나를 패턴 공간에 읽고, 주소가 그 줄을 고르는 하위 명령을 모두 차례로 적용하고, 패턴 공간을 표준 출력에 씁니다. 그리고 패턴 공간을 비우고 다음 줄로 넘어갑니다. 어떤 하위 명령은 보관 공간에 패턴 공간의 전부나 일부를 저장해 두었다가 나중에 꺼내 씁니다.
명령에 주소(줄 번호나 검색 패턴)가 있으면 그 주소의 줄에만 적용하고, 없으면 모든 줄에 적용합니다.
주소
주소는 10진 줄 번호, 입력의 마지막 줄을 가리키는 $(달러 기호), 또는 문맥 주소입니다. 문맥 주소는 ed 명령의 정규 표현식과 비슷하지만 다음이 다릅니다.
- 패턴의 구분 문자를 고를 수 있습니다. 일반 형태는
\?pattern?이며,?(물음표) 자리에 공백과 줄바꿈을 뺀 현재 로캘의 어떤 문자든 쓸 수 있습니다.\(백슬래시)는 첫 구분 문자 앞에만 필요합니다. 기본 형태는/pattern/이며 이때는 백슬래시가 필요 없습니다. \n은 패턴 공간 안의 줄바꿈 문자(끝의 줄바꿈은 제외)와 맞습니다..(마침표)는 끝의 줄바꿈을 뺀 아무 문자와 맞습니다. 줄 가운데의 줄바꿈과 맞을 수 없는ed와 달리,sed는 패턴 공간 안의 줄바꿈과도 맞을 수 있습니다.
주소를 받는 명령에는 다음 규칙이 적용됩니다.
- 주소가 없는 명령 줄은 모든 줄을 고릅니다.
- 문맥 형식 주소가 하나인 명령 줄은 그 주소와 맞는 줄을 모두 고릅니다.
- 쉼표로 나눈 주소가 둘인 명령 줄은 첫 주소와 맞는 줄부터 둘째 주소와 맞는 다음 줄까지 범위 전체를 고릅니다. 둘째 주소가 처음 고른 줄 번호 이하의 숫자면 한 줄만 고릅니다. 그 뒤 다시 첫 주소를 찾으며 되풀이합니다.
영어 원문
The sed command modifies lines from the specified File parameter according to an edit script and writes them to standard output. The sed command includes many features for selecting lines to be modified and making changes only to the selected lines.
The sed command uses two workspaces for holding the line being modified: the pattern space, where the selected line is held; and the hold space, where a line can be stored temporarily.
An edit script consists of individual subcommands, each one on a separate line. The general form of sed subcommands is the following:
[address-range] function[modifiers]
The sed command processes each input File parameter by reading an input line into a pattern space, applying all sed subcommands in sequence whose addresses select that line, and writing the pattern space to standard output. It then clears the pattern space and repeats this process for each line specified in the input File parameter. Some of the sed subcommands use a hold space to save all or part of the pattern space for subsequent retrieval.
When a command includes an address (either a line number or a search pattern), only the addressed line or lines are affected by the command. Otherwise, the command is applied to all lines.
An address is either a decimal line number, a $ (dollar sign), which addresses the last line of input, or a context address. A context address is a regular expression similar to those used in the ed command except for the following differences:
- You can select the character delimiter for patterns. The general form of the expression is:
\?pattern?
where ? (question mark) is a selectable character delimiter. You can select any character from the current locale except for the space or new-line character. The \ (backslash) character is required only for the first occurrence of the ? (question mark). The default form for the pattern is the following:
/pattern/
A \ (backslash) character is not necessary.
The
\nsequence matches a new-line character in the pattern space, except the terminating new-line character.A . (period) matches any character except a terminating new-line character. That is, unlike the ed command, which cannot match a new-line character in the middle of a line, the sed command can match a new-line character in the pattern space.
Certain commands called addressed commands allow you to specify one line or a range of lines to which the command should be applied. The following rules apply to addressed commands:
A command line without an address selects every line.
A command line with one address, expressed in context form, selects each line that matches the address.
A command line with two addresses separated by commas selects the entire range from the first line that matches the first address through the next line that matches the second. (If the second address is a number less than or equal to the line number first selected, only one line is selected.) Thereafter, the process is repeated, looking again for the first address.
플래그
| 플래그 | 설명 |
|---|---|
-e Script |
Script 변수를 편집 스크립트로 씁니다. -e 하나만 쓰고 -f가 없으면 -e를 생략할 수 있습니다. |
-f ScriptFile |
ScriptFile을 편집 스크립트로 씁니다. File 매개변수에 적용할 편집 명령을 미리 적어 둔 파일입니다. |
-n |
보통 표준 출력에 쓰는 내용을 모두 쓰지 않습니다. |
-u |
출력을 버퍼에 모으지 않고 바로 표시합니다. 기본은 버퍼를 씁니다. |
참고: -e와 -f는 여러 번 지정할 수 있습니다. 모든 하위 명령은 어디서 왔든 지정한 순서대로 스크립트에 더해집니다.
영어 원문
-eScript — Uses the Script variable as the editing script. If you are using just one-eflag and no-fflag, the-eflag can be omitted.-fScriptFile — Uses the ScriptFile variable as the source of the edit script. The ScriptFile variable is a prepared set of editing commands applied to the File parameter.-n— Suppresses all information normally written to standard output.-u— Displays the output in an unbuffered mode. When this flag is set, the sed command displays the output instantaneously instead of buffering the output. The default is buffered mode.- Note: You can specify multiple
-eand-fflags. All subcommands are added to the script in the order
specified, regardless of their origin.
sed 하위 명령
하위 명령 앞 괄호의 숫자는 그 하위 명령이 받을 수 있는 주소의 최대 수입니다.
참고:
a\,c\,i\의 Text는 마지막 줄을 뺀 모든 줄을\(백슬래시)로 끝내 줄바꿈을 막으면 여러 줄로 이어 쓸 수 있습니다. Text 안의 백슬래시는s명령의 바꿀 문자열 안 백슬래시처럼 다루며, 스크립트 줄마다 앞의 빈칸·탭이 지워지는 것을 막는 데 쓸 수 있습니다. RFile과 WFile은 명령 줄의 끝에 와야 하고, 앞에 빈칸이 정확히 하나 있어야 합니다. WFile은 처리를 시작하기 전에 만들어집니다.sed는 패턴 파일에서 하위 명령을 999개까지 처리할 수 있습니다.
| 주소 수 | 하위 명령 | 설명 |
|---|---|---|
| 1 | a\Text |
다음 입력 줄을 읽기 전에 Text를 출력에 넣습니다. |
| 2 | b[label] |
label이 붙은 : 명령으로 갑니다. label이 비어 있으면 스크립트 끝으로 갑니다. |
| 2 | c\Text |
패턴 공간을 지웁니다. 주소가 0개나 1개일 때, 또는 주소 두 개 범위의 끝에서 Text를 출력에 넣고 다음 주기를 시작합니다. |
| 2 | d |
패턴 공간을 지우고 다음 주기를 시작합니다. |
| 2 | D |
패턴 공간의 처음부터 첫 줄바꿈까지를 지우고 다음 주기를 시작합니다. |
| 2 | g |
패턴 공간의 내용을 보관 공간의 내용으로 바꿉니다. |
| 2 | G |
보관 공간의 내용을 패턴 공간 뒤에 덧붙입니다. |
| 2 | h |
보관 공간의 내용을 패턴 공간의 내용으로 바꿉니다. |
| 2 | H |
패턴 공간의 내용을 보관 공간 뒤에 덧붙입니다. |
| 1 | i\Text |
다음 줄을 패턴 공간에 읽기 전에 Text를 표준 출력에 씁니다. |
| 2 | l |
패턴 공간을 눈으로 구별할 수 있는 형태로 표준 출력에 씁니다(아래 설명). |
| 2 | n |
기본 출력을 끄지 않았으면 패턴 공간을 표준 출력에 쓰고, 패턴 공간을 다음 입력 줄로 바꿉니다. |
| 2 | N |
다음 입력 줄을 줄바꿈 문자를 사이에 두고 패턴 공간 뒤에 덧붙입니다(현재 줄 번호가 바뀝니다). 두 줄에 걸친 패턴을 찾을 때 씁니다. |
| 2 | p |
패턴 공간을 표준 출력에 씁니다. |
| 2 | P |
패턴 공간의 처음부터 첫 줄바꿈까지를 표준 출력에 씁니다. |
| 1 | q |
스크립트 끝으로 갑니다. 새 주기를 시작하지 않습니다. |
| 2 | r RFile |
RFile의 내용을 읽어, 다음 입력 줄을 읽기 전에 출력에 넣습니다. |
| 2 | s/pattern/replacement/flags |
패턴 공간에서 pattern이 처음 나오는 곳을 replacement로 바꿉니다(아래 설명). |
| 2 | t[label] |
마지막으로 입력 줄을 읽었거나 t를 실행한 뒤에 바꾸기가 한 번이라도 있었으면 :label로 갑니다. label이 없으면 스크립트 끝으로 갑니다. |
| 2 | w WFile |
패턴 공간을 WFile에 덧붙입니다. |
| 2 | x |
패턴 공간과 보관 공간의 내용을 맞바꿉니다. |
| 2 | y/pattern1/pattern2/ |
pattern1의 문자를 모두 pattern2의 대응하는 문자로 바꿉니다. 두 패턴의 문자 수가 같아야 합니다. 줄바꿈 문자는 \n으로 나타냅니다. |
| 2 | !sed-cmd |
주소가 고르지 않은 줄에만 지정한 하위 명령을 적용합니다. |
| 0 | :label |
b와 t가 가리킬 분기점을 표시합니다. label은 8바이트 이하입니다. |
| 1 | = |
현재 줄 번호를 한 줄로 표준 출력에 씁니다. |
| 2 | {subcmd } |
중괄호 안의 하위 명령을 묶습니다. |
| 0 | (빈 명령) | 빈 명령은 무시합니다. |
| 0 | # |
#과 그 줄의 나머지를 주석으로 무시합니다. 예외로, 스크립트 파일의 첫 줄에서 # 다음 글자가 n이면 기본 출력을 끕니다(#n 뒤의 나머지는 무시). |
l 하위 명령
원문은 l을 두 번 설명합니다. 앞의 설명은 "표시할 수 없는 문자를 4자리 16진수로 보여 주고 긴 줄을 접는다"이고, 뒤의 설명은 다음과 같습니다.
\\,\a,\b,\f,\r,\t,\v문자는 해당 이스케이프 시퀀스로 씁니다.- 인쇄할 수 없는 문자는 바이트마다 백슬래시가 앞에 붙은 3자리 8진수로 씁니다(상위 바이트부터). 다중 바이트 문자에도 이 형식을 씁니다.
- 긴 줄은 72번째 열에서 접고, 접은 곳은 백슬래시와 줄바꿈으로 표시합니다. 줄마다 끝에
$를 붙입니다.
s 하위 명령의 flags
s 뒤에 오는 문자라면 공백과 줄바꿈을 빼고 무엇이든 /(슬래시) 대신 구분자로 쓸 수 있습니다. 패턴은 ed 명령의 Pattern Matching 절을 참고합니다. flags는 다음을 0개 이상 씁니다.
| flag | 뜻 |
|---|---|
g |
첫 번째만이 아니라 겹치지 않는 모든 곳을 바꿉니다. |
n |
pattern이 n번째 나오는 곳만 바꿉니다. |
p |
바꾸기가 있었으면 패턴 공간을 표준 출력에 씁니다. |
w WFile |
바꾸기가 있었으면 패턴 공간을 WFile에 덧붙입니다. 이 스크립트가 앞서 쓴 적이 없으면 WFile을 만듭니다. |
영어 원문
The sed command contains the following sed script subcommands. The number in parentheses preceding a subcommand indicates the maximum number of permissible addresses for the subcommand.
Note:
The Text variable accompanying the
a\,c\, andi\subcommands can continue onto more than one line, provided all lines but the last end with a\(backslash) to quote the new-line character. Backslashes in text are treated like backslashes in the replacement string of an s command and can be used to protect initial blanks and tabs against the stripping that is done on every script line. The RFile and WFile variables must end the command line and must be preceded by exactly one blank. Each WFile variable is created before processing begins.The sed command can process up to 999 subcommands in a pattern file.
- (1)
a\Text— Places the Text variable in output before reading the next input line. - (2)b[label] — Branches to the : command bearing the label variable. If the label variable is empty, it branches to the end of the script.
- (
2)c\Text— Deletes the pattern space. With 0 or 1 address or at the end of a 2-address range, places the Text variable in output and then starts the next cycle. - (2)d — Deletes the pattern space and then starts the next cycle.
- (2)D — Deletes the initial segment of the pattern space through the first new-line character and then starts the next cycle.
- (2)g — Replaces the contents of the pattern space with the contents of the hold space.
- (2)G — Appends the contents of the hold space to the pattern space.
- (2)h — Replaces the contents of the hold space with the contents of the pattern space.
- (2)H — Appends the contents of the pattern space to the hold space.
- (
1)i\Text— Writes the Text variable to standard output before reading the next line into the pattern space. - (2)l — Writes the pattern space to standard output showing nondisplayable characters as 4-digit hexadecimal values. Long lines are folded.
- (2)l — Writes the pattern space to standard output in a visually unambiguous form. The characters
\\\,\\a,\\b,\\f,\\r,\\t, and\\vare written as the corresponding escape sequence. Non-printable characters are written as 1 three-digit octal number (with a preceding backslash character) for each byte in the character (most significant byte first). This format is also used for multibyte characters. This subcommand folds long lines. A backslash followed by a new-line character indicates the point of folding. Folding occurs at the 72nd column position. A$(dollar sign) marks the end of each line. - (2)n — Writes the pattern space to standard output if the default output is not suppressed. It replaces the pattern space with the next line of input.
- (2)N — Appends the next line of input to the pattern space with an embedded new-line character (the current line number changes). You can use this to search for patterns that are split onto two lines.
- (2)p — Writes the pattern space to standard output.
- (2)P — Writes the initial segment of the pattern space through the first new-line character to standard output.
- (1)q — Branches to the end of the script. It does not start a new cycle.
- (2)r RFile — Reads the contents of the RFile variable. It places contents in output before reading the next input line.
- (2)s/pattern/replacement/flags — Substitutes the replacement string for the first occurrence of the pattern parameter in the pattern space. Any character that is displayed after the s subcommand can substitute for the / (slash) separator except for the space or new-line character. See the Pattern Matching section of the ed command. The value of the flags variable must be zero or more of: g Substitutes all non-overlapping instances of the pattern parameter rather than just the first one. n Substitutes for the n-th occurrence only of the pattern parameter. p Writes the pattern space to standard output if a replacement was made. w WFile Writes the pattern space to the WFile variable if a replacement was made. Appends the pattern space to the WFile variable. If the WFile variable was not already created by a previous write by this sed script, the sed command creates it.
- (2)tlabel — Branches to the :label variable in the script file if any substitutions were made since the most recent reading of an input line execution of a t subcommand. If you do not specify the label variable, control transfers to the end of the script.
- (2)wWFile — Appends the pattern space to the WFile variable.
- (2)x — Exchanges the contents of the pattern space and the hold space.
- (2)y/pattern1/pattern2/ — Replaces all occurrences of characters in the pattern1 variable with the corresponding pattern2 characters. The number of characters in the pattern1 and pattern2 variables must be equal. The new-line character is represented by
\n. - (2)!sed-cmd — Applies the specified sed subcommand only to lines not selected by the address or addresses.
- (0):label — Marks a branch point to be referenced by the b and t subcommands. This label can be any sequence of eight or fewer bytes.
- (1)= — Writes the current line number to standard output as a line.
- (2) — Groups subcommands enclosed in (braces).
- (0) — Ignores an empty command.
- (0)# — The "#" and the remainder of the line are ignored (treated as a comment), with one exception. For the first line of a script file, if the character after the # is an n, the default output is suppressed. The rest of the line after the #n is ignored.
종료 상태
| 값 | 뜻 |
|---|---|
| 0 | 잘 끝났습니다. |
| 0보다 큼 | 오류가 났습니다. |
영어 원문
This command returns the following exit values:
- 0 — Successful completion.
>0— An error occurred.
예제
- 전체를 바꿉니다.
chap1파일의happy를 모두enchanted로 바꿔chap1.new에 넣습니다.s하위 명령 끝의g는 한 줄에서 바꿀 수 있는 만큼 모두 바꾸라는 뜻이며,g가 없으면 줄마다 처음 나오는happy만 바꿉니다.sed는 필터로 동작해 표준 입력이나 명령줄의 파일에서 글을 읽고, 고쳐서 표준 출력에 씁니다. 대부분의 편집기와 달리 원본 파일을 바꾸지 않으므로 파이프라인에서 쓰기 좋습니다.
sed "s/happy/enchanted/g" chap1 >chap1.new
- 파이프라인에서 필터로 씁니다.
chap2를 인쇄하기 전에 쪽 번호를 괄호로 감쌉니다.pr이 쪽마다 머리글과 쪽 번호를 넣고,sed가 쪽 번호를 괄호로 감싸고,enq가 인쇄합니다. 패턴/Page *[0-9]*$/는 줄 끝의 쪽 번호와 맞고,s하위 명령이 그것을(&)로 바꿉니다.&는 맞은 쪽 번호입니다.
pr chap2 | sed "s/Page *[0-9]*$/(&)/" | enq
- 파일의 고른 줄만 표시합니다.
chap3에서food가 든 줄만 보여 줍니다. 보통sed는 고친 뒤 모든 줄을 표준 출력에 복사하는데,-n이 이를 막고p같은 하위 명령으로 원하는 부분만 씁니다.-n이 없으면 모든 줄이 나오고food가 든 줄은 두 번 나옵니다.
sed -n "/food/p" chap3
- 복잡한 편집을 합니다. 복잡한 일은
sed스크립트 파일을 만들어 씁니다. 스크립트를 미리 시험하고 고칠 수 있고, 다른 파일을 편집할 때 다시 쓸 수 있습니다. 스크립트 파일은 대화형 텍스트 편집기로 만듭니다.
sed -f script.sed chap4 >chap4.new
sed스크립트 파일의 예입니다.\(백슬래시)로 끝나는 줄을 다음 줄과 잇습니다.
:join
/\\$/{N
s/\\\n//
b join
}
먼저 패턴 /\\$/가 \로 끝나는 줄을 골라 중괄호 안의 명령 묶음에 넘깁니다. N이 다음 줄을 줄바꿈과 함께 덧붙이고, s/\\\n//가 \와 사이의 줄바꿈을 지웁니다. 마지막으로 b join이 :join으로 돌아가 이어 붙인 줄 끝에 다시 \가 있는지 확인합니다. 이 분기가 없으면 이은 줄을 쓰고 다음 줄을 읽은 뒤에야 둘째 \를 확인합니다.
참고: N은 더 읽을 입력이 없으면(파일 끝을 읽으면) 패턴 공간을 출력하지 않고 바로 멈춥니다. 그래서 입력의 마지막 줄이 \로 끝나면 그 줄은 출력되지 않습니다.
- 기존 파일
oldfile을 새 파일newfile로 복사하면서testpattern문자열을 셸 변수$REPL의 내용으로 모두 바꿉니다.
cat oldfile | sed -e "s/testpattern/$REPL/g" > newfile
- 입력 파일에서
A를a로,B를b로,C를c로, 줄바꿈을 모두Z로 바꿉니다.command.file은 스크립트 파일,input.file은 입력 파일입니다.
sed -f command.file input.file
command.file의 내용:
y/ABC\n/abcZ/
같은 일을 다음 명령으로도 할 수 있습니다.
sed "y/ABC\n/abcZ/" input.file
관련 명령어: awk, ed, grep
영어 원문
- To perform a global change, enter:
sed "s/happy/enchanted/g" chap1 >chap1.new
This command sequence replaces each occurrence of the word happy found in the file chap1 with the word enchanted. It puts the edited version in a separate file named chap1.new. The g character at the end of the s subcommand tells the sed command to make as many substitutions as possible on each line. Without the g character, the sed command replaces only the first occurrence of the word happy on a line. The sed command operates as a filter. It reads text from standard input or from the files named on the command line (chap1 in this example), modifies this text, and writes it to standard output. Unlike most editors, it does not replace the original file. This makes the sed command a powerful command when used in pipelines.
| | |
- To use the sed command as a filter in a pipeline, enter:
pr chap2 | sed "s/Page *[0-9]*$/(&)/" | enq
This command sequence encloses the page numbers in parentheses before printing the file chap2. The pr command puts a heading and page number at the top of each page, then the sed command puts the page numbers in parentheses, and the enq command prints the edited listing. The sed command pattern /Page *[0-9]*$/ matches page numbers that appear at the end of a line. The s subcommand changes this to (&), where the & stands for the page number that was matched.
- To display selected lines of a file, enter:
sed -n "/food/p" chap3
The sed -n displays each line in the file chap3 that contains the word food. Normally, the sed command copies every line to standard output after it is edited. The -n flag stops the sed command from doing this. You then use subcommands like p to write specific parts of the text. Without the -n flag, this example displays all the lines in the file chap3, and it shows each line containing food twice.
- To perform complex editing, enter:
sed -f script.sed chap4 >chap4.new
This command sequence creates a sed script file when you want to do anything complex. You can then test and modify your script before using it. You can also reuse your script to edit other files. Create the script file with an interactive text editor.
- A sample sed script file:
:join /\\$/{N s/\\\n// b join }
This sed script joins each line that ends with a \ (backslash) to the line that follows it. First, the pattern /\\$/ selects a line that ends with a \ for the group of commands enclosed in {} (braces). The N subcommand then appends the next line, embedding a new-line character. The s/\\\n// deletes the \ and embedded new-line character. Finally, b join branches back to the label :join to check for a \ at the end of the newly joined line. Without the branch, the sed command writes the joined line and reads the next one before checking for a second \.
Note: The N subcommand causes the sed command to stop immediately if there are no more lines of input (that is, if the N subcommand reads an end-of-file character). It does not copy the pattern space to standard output before stopping. This means that if the last line of the input ends with a \, it is not copied to the output.
To copy an existing file (
oldfile) to a new file (newfile) and replace all occurrences of thetestpatterntext string with the contents of the$REPLshell variable, enter:cat oldfile | sed -e "s/testpattern/$REPL/g" > newfileTo replace all occurrences of A with a, B with b, C with c, and all occurrences of newlines with character Z in the input file, enter:
$ sed -f command.file input.file
where command.file is the script file and input.file is the input file.
$cat command.file
y/ABC\n/abcZ/
Alternatively, the following command can also be executed for the same function: sed "y/ABC\n/abcZ/" input.file
Related information:
awk command ed command grep command Manipulating Strings with sed National Language Support
리눅스(GNU)와 다른 점
| 항목 | AIX 5.3·6.1 | GNU sed |
|---|---|---|
-i |
없음(결과를 다른 파일로 받아 옮김) | 파일을 직접 고침 |
-E·-r |
없음(기본 정규 표현식만) | 확장 정규 표현식 |
| 한 줄 명령 | a\, i\, c\ 뒤에 줄을 바꿔 Text를 씀 |
a text처럼 한 줄로 쓸 수 있음 |
l의 표시 |
원문에 4자리 16진수 설명과 8진수 설명이 함께 있음 | 8진수 |
| 하위 명령 수 | 패턴 파일에서 999개까지 | 제한 없음 |
리눅스에서 sed -i 's/a/b/' file처럼 쓰던 것은 AIX에서 sed 's/a/b/' file > file.new && mv file.new file로 바꿉니다.