SED manual
1.Intuction
Sed (stream editor) provides an editor that will automatically automate editing on UNIX systems, and users do not need to edit directly. User can
With more than 20 different function parameters provided by SED, combining (solution [1]) they complete different editing actions. In addition, due to SED
Editing the file in a behavior unit, it is also a line editor.
General SEDs are most commonly used in editing those that need to continue to overcome some editing actions, such as replacing some strings in the file into another word.
Strings, etc. These modify files compared to the general Unix editor (talk, such as VI, Emacs) to modify files, and SED is more labor-saving.
The following sections will introduce them:
When will I use SED?
Where is SED
Sed can do those editing actions
Sed how work
1.1 When will I use SED?
When modifying the file, if some editing actions are constantly being resumed, these editing actions can be performed with SED automatically. For example, make recaid
The sender's name "Tom" in the 1000 e-mail inside the file is changed to "John". At this time, simply perform a simple SED command on the command column.
Replace the "Tom" string in the file into "john".
Furthermore, when the file requires many different editing actions, SED can perform those different editing actions at a time. For example, SED can perform once
All blank lines in the file are deleted, replace the strings, and add the text entered by the user to the sixth line of the file.
1.2 Where is the SED
The general UNIX system is here to accompany SED. The Sed versions attached to the Different UNIX systems are not the same. If UNIX is used by the reader
There is no SED with an anonymous ftp on the system, and it can be obtained by anonymous ftp:
Phi.sinica.edu.tw:/pub/gnu/gnu
GetE.sinica.edu.tw:/unix/gnu
ftp.edu.tw:/unix/gnu
ftp.csie.nctu.edu.tw:/pub/unix/gnu
ftp.fcu.edu.tw: / pub3 / unix / gnu
Axp350.ncu.edu.tw:/packages/gnu
Leica.ccu.edu.tw: / pub2 / gnu
Mail.ncku.edu.tw: / pub / unix / gnu
Bbs.ccit.edu.tw: / pub1 / unix / gnu
Prep.Ai.mit.edu.tw:/pub/gnu
1.3 SED can do those editing actions
Sed Delete, Change, Add (Append), Insert, Switching Documents, or Read Other
Document to files, also replace (Substuite) them therein, or transformation (TRANFER) in the letter, etc. For example, in the file
Continuous blank lines are deleted into a row, "local" string is replaced into "remote", "t" letters into "T", and the 10th line of information is combined with the 11th information
Wait.
1.4 how to work
As with other UNIX commands, the SED reads the editing file by the standard input and sent out by the standard output. The following figure shows that the SED will travel to "UNIX"
Replace with "UNIX",
In the figure, the above Standard INPUT is the standard input, which is the reading information; Standard Output is the standard output, which is the result of the results;
The following two dashed squares of the intermediate SED block represent the workflow of the SED. Among them, the left dotted block represents the SED to place the standard input data.
Pattern Space, the right dadden block represents the SED to send the data after editing in Pattern Space to the standard output.
In the dotted block, two solid blocks represent Pattern Space and Sed Script, respectively. Among them, Pattern Space is a cockpit area, it is
SED workplace; while Sed Script represents a set of executed editing instructions. In the figure, the left dotted block "UNIX" is placed in Pattern Space by the standard input; then, in the nunet square block, SED SIST is executed Sed Script
Editing instructions S / UNIX / UNIX / (Solution [2]), the result "UNIX" is replaced with "UNIX", then "UNIX" is sent by Pattern Space to
Standard output.
In the above, the SED is in accordance with the editorial instruction of the SED Script when the SED reads a row of information and puts the pattern space.
A pair of data within the Pattern Space executes editing, then, then sent to the standard output in the Pattern Space, then the next line of capital
Refigure it. So far, the above action is reached until all data behavior is read.
SED Manual - 2. Using SED
Http://phi.sinica.edu.tw aspac@phi.sinica.edu.tw (2001-07-29 09:05:00)
The SED command column can be divided into edit instructions and file files. Among them, the editing instruction is responsible for controlling all editing work; the file file represents the file being processed.
The editing instruction of the SED is constructed by the address and function (function), where the SED is used to use its location parameters.
Decide the object edited; and use its function parameters (solution [3]).
In addition, the SED editing instruction can be executed within the file in addition to the command column. The difference is only to execute on the command column,
You must add option -E; and within the file (Solution [4]), you only need to add option -f before its file name. In addition, the SED executive editing instruction is in accordance with it
They are in the order or in the order of the command.
In the following sections, the editing instructions, SED editing instructions on the command column will be described, and the editing instruction, the editing instruction inside the file, execute the editing of multiple files, and
Perform the SED output control.
2.1 Execute the editing instruction on the command column
2.2 SED editing instruction
2.3 Editing instructions within the file
2.4 Enforcement of multiple files
2.5 Execute SED Output Control
2.1. Execute the editing instruction on the command column
When the editing instruction (refer to [Section 2.2]) is executed on the command column, you must add option -e. The format of its command is as follows:
Sed -E 'Editorial Directive 1' -e 'Editor 2' ... file file
All editing instructions are followed by option -e and placed in two "'" special characters. In addition, the execution of the editing instruction on the command is
Left and right.
Generally, when the editing instruction is not allowed, the user usually executes them directly on the command. For example, delete 1 to 10 lines of information within YEL.DAT, and
The "Yellow" string in the text is changed to the "Black" string. At this point, the editing instruction can be executed directly on the command, and the command is as follows:
Sed -E '1,10d' -e 'S / Yellow / Black / g' YEL.DAT
In the command, editing instructions '1, 10d' (solution [5]) is executed to delete 1 to 10 lines of data; editing instructions 's / yellow / black / g' (solution [6]),
"YELLOW" string replacement (Substuite) is "Black" string.
2.2 Editorial Directive of SED
The format of the SED editing instruction is as follows:
[address1 [, address2]] function [argument]
Among them, the address parameter address1, address2 is the number of rows or REGULAR Expression strings, indicating the information line executed;
Number Function [argument] is the end of the SED, indicates the execution editing action.
The following two sections will carefully describe the representations of the location parameters and which function parameters are available.
2.2.1 Address parameters actually, the location parameter representation is only the data to be edited, replacing them with the number of rows or strings thereof. Let's take a few examples
Description (The instruction is used as a function parameter d (refer to [section4.2]):
Delete the 10th line of information in the file, the command is 10d.
When you delete the information line containing the "Man" string, the instruction is / man / d.
Delete the 10th line to 200 row of information in the file, the instruction is 10,200d.
Deleting the 10th line of the file to the data line with the "Man" string, the instruction is 10, / man / d.
Next, the representation of the order parameters in the order parameters, and the representation of the order parameters in the command (also as an example of the function parameter D).
The content of the location parameters:
Address for ten-in number: This number represents the number of rows. When the instruction is executed, the editing action of the function parameter indicated by the data that meets the number of this row is executed. E.g ,
Delete the 15th line of data in the data file, the instruction is 15d (refer to [Section4.2]). Its push, such as the Mth data in the data file,
The instruction is MD.
REGULAR Expression (Refer to [Appendix A]):
When the data line has a string representing regular expression, the editing action indicated by the function parameter is executed. In addition, in
"/" must be added before and after regonational. For example, the instruction is /t.*T/d, indicating that all data lines containing two "T" letters. among them , "."
Represents any font; "*" indicates that its preamble can be repeatedly blessed, and they combine ". *" Means any string between the two "T" letters.
Number of location parameters: In the instruction, when there is no location parameter, the editing action indicated by the full data line execution function parameters; when only one address
When the parameter, it means that only the information lines that meet the location is edited; when there are two address parameters, such as address1, address2, indicate the information area execution
Edit, Address1 represents the starting data line, and the Address2 represents the end of the data line. For the above, the following example is described.
For example, instructions are
di
It means deleting all the information in the file.
For example, instructions are
5D
It represents the fifth line of information within the file.
For example, instructions are
1, / apple / d
It indicates that the data area is deleted, and the first line in the first line has a "Apple" string of "Apple" strings.
For example, instructions are
/ apple /, / orange / d
It indicates that the data area is deleted, and the "Apple" string is contained in the file to the information line containing the "Orange" string.
2.2.2 Have those functions (function)
The functions of all SED function parameters (refer to [Chapter 4]) are described in the next page.
Function parameter function
: Label establishes the position of the Script File instructions.
# 建立 解
The {} collection has instructions with the same address parameters.
! Do not perform function parameters.
= Printing the number of data (LINE NUMBER).
a Add the information entered by the user.
b Label jumps to the executed instruction to the reference location established.
c Substituting information entered by the user.
D Delete information.
D Delete the first newline letter before the Pattern Space.
G copy information from Hold Space.
G Add information from Hold Space to Pattern Space.
H copy information from Pattern Space to Hold Space.
H Add information from Pattern Space to Hold Space.
l Printing the NonPrinting Character in the information with the ASCII code.
i inserts the information line that adds the user input.
N read into the next information.
N Add the next information to Pattern Space.
P Printing information.
P Prints the information before the first newline letter in Pattern Space.
Q jumps out of the SED editing.
r read into its file content. s replace the string.
T Label performs a replacement editing instruction. If replaced into cow P>, you skip the editing instruction to: Label is executed.
W Write information to its file.
X Exchange Hold Space and Pattern Space content.
Y Transform Volume.
Although the SED is only the function of the basic editing function, the SED is only the function of the basic editing function, but it can also make
Sed has completed most of the editing tasks.
2.3 Editing instructions within the file
When the execution instruction is made, it is very confusing on the command column. At this time, these instructions can be stored in the file.
(For example, file name is Script_File, use option -f script_file, let SED perform editing instructions in the script_file. Whim
The order is as follows:
Sed -f script_file file file
Where, the order in which the editing instruction inside the Script_File is executed is upward. For example, an example of the previous section can be changed to the following command:
Sed -f ysb.scr Yel.dat
Among them, the YSB.sc RR file is as follows:
1,10d
S / Yellow / Black / g
In addition, in the command column, it is mixed using the options -E and -F, the SED execution instruction sequence is still the left to right of the command column, such as executing to -f
The instructions in the file are executed by top.
2.4 Execute the editing of multiple file files
On the SED command column, you can perform editing multiple file files at a time, they follow the editing instruction. For example, replacement
"YELLOW" string in white.dat, red.dat, black.dat is "Blue", whose command is as follows:
Sed -e 's / yelow / blue / g' white.dat red.dat black.dat
When the above command is executed, the sed is in the order of white.dat, red.dat, black.dat, executes editing instructions S / YELLOW / Blue / (see [Section 4.1],
String replacement.
2.5. Control of execution output
The options -n (solution [7]) on the command column indicates that the output is controlled by the editing instruction. Understanding the content of the previous chapter, the SED will "automatically" will
Pattern Space is delivered to a standard output file. However, by option -n, the "automatic" action can be changed to "passive" by it.
Edit instructions (solution [8]) determine if the result is output.
As can be seen from the above, the option -n must cooperate with the editing instruction, otherwise the result cannot be obtained. For example, printing the White.dat file contains "White"
The data line of the string, the command is as follows:
Sed -n -e '/ white / p' White.dat
In the above command, the option -n is combined with the editing instruction / white / p (refer to [Section4.6]). Where option -n will output control
Move to edit instructions; / White / P prints the data line with the "White" string.
SED Manual - 3. Case
http://phi.sinica.edu.tw aspac@phi.sinica.edu.tw (2001-07-29 13:05:00)
Generally, in the actual use of the editor, the strings, move, delete, and the search data line are often required to perform the replacement file.
Of course, all the general conversation editors (such as VI, EMACS) can do the above functions, but once there is a large number of above editing requirements,
Use them to edit it very no efficiency. This chapter will explain how to automatically perform these editing functions in SED in a way. In addition, in this chapter
In the example, the needs of the documents are described in the following manner:
Use the document ... Information, execute ...
As such, the purpose is to quickly transfer them to edit instructions. Among them, "... Information" part, the address parameters in the command;
"Execute the ... Action" part, then the transfer is a function parameter representation. In addition, when "executing ... Actions" is represented by several function parameters, "{" and "}" can be utilized (解 [9]), and the instruction form is as follows:
Added parameter {
Function parameters 1
Function parameter 2
Function parameter 3
.
:
}
The above instructions are indicated, and the information indicated by the order parameters will be executed sequentially, and the function parameter 1, the function parameter 2, and the function parameter 3 is executed.
The following sections, separately illustrate the SED replacement information, mobile, delete information, and commands for searching information.
3.1 Replace the information in the file
3.2 Information in the moving document
3.3 Delete information in the file
3.4 Information in searching documents
3.1 Replace the information in the file
SED can replace the string, data line, and even the data area in the file. Among them, the function parameters in instructions in the replacement string are s (refer to [section4.1]);
Indicates that the function parameters in the instructions of the replacement information, or the data area are c (refer to [section4.5]). The above case will be described below. Above
The following three examples are explained.
For example, the "PHI" string in the data line containing the "Machine" string is replaced with the "Beta" string. Its command is as follows:
Sed -e '/ Machine / S / PHI / BETA / G' INPUT.DAT (after the file file is represented by Input.dat)
Example II. Row the 5th line of the file, replace the sentence "Those Who in Quarrels Interpose, Must Offen Wipe A Bloody Nose.".
Its command is as follows
Sed -e '
5C
Those Must OFTEN WIPE A BLOODY NOSE.
'Input.dat
Example III. Replace the data area of 1 to 100 lines in the document to the following two lines of information:
How are you?
Data be deleted!
Then there is a command.
Sed -e '
1,100c
How are you?
Data be deleted!
'Input.dat
3.2 Information in the moving document
The user can use the material parameter W (refer to [Section4.9]) to save the file into its file with the function parameter W (refer to [Section4.9]).
Or move its file content to the file with function parameter R (refer to [Section4.8]). Hold Space is the temporary of the SED used to temporarily save Pattern Space
Registers, when the SED performs function parameters h, h (refer to [section4.19]), will temporarily save the Pattern Space information to Hold Space;
When parameter x, g, g (refer to [Section4.22]), the temporary data will be taken to Pattern Space. Three examples will be given below.
Example 1. Plate the top 100 in the file to move to the 30000 data in the file. Its command is as follows:
Sed -f mov.scr file file
The content of the Mov.scr file is
1,100 {
Hide
di
}
300g
among them ,
1,100 {
Hide
di
}
It indicates that the top 100 data in the file is stored first (refer to [Section4.19]) after Hold Space;
300g (refer to [section4.22])
Indicates that the information within the Hold Space is added to the 30000 data in the file.
Example II. Documentation of the "PHI" string in the file is moved to the Mach.inf file. Its command is as follows:
Sed -e '/ pHI / w Mach.inf' file file
Example III. Wiping the contents of the Mach.inf to the document containing the "Beta" string. Its command is as follows:
Sed -e '/ beta / r mach.inf' file file
In addition, since the SED is a stream (refer to the [Section 1.4]) editor, the file data after the theoretically output is impossible to move back. 3.3 Delete information in the file
Because the SED is an editor, SED is easy to delete individual data lines or the entire data area. General use of function parameters D (refer to [Section4.2]) or
D (Refer to [Section4.17]). Next, two examples will be described below.
Remove all blank lines in the file. The order is listed as
Sed -E '/ ^ $ / d' file file
Regular Expression (Solution [Appendix A]), ^ $ Represents a blank line. Among them, ^ limits the back strings must be in the order; $ Limit its preamble must be at the end.
Remove them in a row of consecutive blank lines. The order is listed as
Sed -E '/ ^ $ / {
N
/ ^ $ / D
} 'File file
Among them, the function parameter n (refer to [Section4.16]) indicates that the next line of marks is added to the Pattern Space. Function parameters / ^ $ / D,
When adding a blank line, remove the first row of blank lines, and the remaining blank line will re-execute the instruction. The instruction is re-executed once, delete a line of blank lines,
Such a consecutive blank row, the last blank line is mainly left until the blank line is added.
3.4 Information in searching documents
The SED can perform a function similar to the UNIX command GREP. In theory, Regular Expression can be used (refer to [Appendix A]). For example, put the files
There are "Gamma" strings of data line output. Then its command is listed below:
Sed -n -e '/ gamma / p' file file
However, the SED is a line editor, and its search is basically in units of acts. Therefore, when some strings are dismantled into two parts by a line, a general method
It is not feasible. At this point, you must search for these information in a two-line manner. Its situation is as follows:
Example. Output of the "Omega" string in the file. Its command is as follows
Sed -f gp.scr file file
The contents of the GP.SCR file are as follows:
/ OMEGA / B
N
hide
S /. //
/ OMEGA / B
G
Di
In the above SED Script (Solution [10]), the Case STATEMENT structure in the CASE STAT is formed by the function parameter B, so that the SED can process the same respectively.
The information is included in the "Omega" string; when the "Omega" string is detached, there is no "Omega" string in the data. Next, according to the above
In this case, Sed Script is discussed below.
When the data is included in "Omega", the editing instruction is executed.
/ OMEGA / B
It means that when the data is included in the "Omega" string, the SED does not need to perform the following instructions, and output it directly.
If there is no "Omega" in the data, the editing instruction is executed.
N
hide
S /. //
/ OMEGA / B
Among them, the function parameter n (refer to [Section 4.16]), which means reading the next line of data into the Pattern Space contains two lines of data before and after. Function
Number h (refer to [Section 4.19]), which means that the front and rear two lines of the Pattern Space are stored in Hold Space. Function parameter S /. //, it
Indicates that two lines of data in the Pattern Space (solution [11]) are in one line. / OMEGA / B, it means if the following information is included in the "Omega" word
String, you don't have to execute the instructions after it, and automatically output this information;
When the following information still does not include "Omega", the editing instruction is executed as follows.
G
Di
Among them, the function parameter g (refer to [Section4.21]), which means putting the two lines of the two lines before Hold Space back to Pattern Space. Function parameters
D (Refer to [Section4.17]), it indicates the first line of data in the two lines of information, and lets the remaining information, re-execute the Sed Script. Thus, the string of the data or the string in the line can be used to search.
SED Manual - 4. Introduction Function Parameters
http://phi.sinica.edu.tw aspac@phi.sinica.edu.tw (2001-07-30 07:00:00)
This chapter will introduce all the function parameters provided by all SEDs in a function parameter, including
| S | D | A | I | C | P | L | R | W | Y |! | N | q | = | # | n | d | p | h | h | g | g | x | b | t |
In addition, in each section, the function parameter function is first introduced, followed by the format of the function parameters and the location parameters, which also describes
SED work scenario is performed.
4.1 s
Function parameter s represents the Substitution file internal string. The format of its instructions are as follows:
[address1 [address2]] s / pattern / replacemen / [flag]
The following description is described in the above format:
Function parameter S is up to two address parameters.
About "S / Pattern / Replacement / [FLAG]" (Solution [12]) has the following description:
Pattern: It is a REGULER EXPRESSION string. It represents the string to be replaced in the file.
Replacement: It is a general string. But there is a special meaning of the following character:
&: Represents Premature Pattern strings. E.g
Sed -e 's / test / & my car /' data file name
In the instruction, & represents "TEST" in the Pattern string. Therefore, after execution, the "TEST" of the data file is replaced with "Test My Car".
: The string enclosed by the nth (,) (refer to [Appendix A]) in Pattern. E.g
Sed -E 'S / (TEST) (MY) / [2 3 1] /' Data file name
In the instruction, 1 means "test", 2 indicates "MY", 1 means "car" string. Therefore, after execution, the "Test My Car" of the data file is replaced.
"[My car test]".
: Can be used to restore some of the meaning of special symbols (such as the & and) itself, or use it to represent the wrap.
Flag: Mainly used to control some replacement:
When the FLAG is G, the representative replaces all strings that match (MATCH).
When the FLAG is a ten-in number M, represents the square string in line in the row.
When Flag is P, after replacing the first string that conforms to Pattern, the data outputs the standard output file.
When Flag is W WFILE, after replacing the first string that conforms to Pattern, it is output to the WFILE file (if WFILE does not exist,
Re-open the file named WFile).
When there is no FLAG, the first string of the data in line with Pattern is replaced by the Replacement string.
Delimiter: "/" in "/" / "" is used as a Delimiter. In addition to blank, there are newlines,
Users can use any character as Delimiter. For example, the following editing instruction
S # / usr # / usr1 # g
In the above command, Verb | # | is Delimiter. If you use "/" to do DELIMITER, the SED will put Pattern and Replacement "/"
An error occurred as Delimiter.
example:
Title: Replace Input.dat file (there is no special specification, assume that the file file is "1996" string into "1997", and the data line is stored in Year97.dat.
Description: Use the function parameter S instruction to replace the "1996" string into "1997", and use the Flag W in s argument to indicate the SED will be replaced.
The data line is deposited in Year97.dat.
SED command column:
Sed -E 'S / 1996/1997 / W Year97.dat' Input.dat
4.2 D
The function parameter D represents the delete data line, its instruction format is as follows:
[address1 [, address2]] d
The following description is described in the above format:
The function parameter d is up to two address parameters.
The SED performing the deletion action is as follows:
Remove the information that meets the order parameters in Pattern Space.
Read the next information into the Pattern Space.
Re-execute the SED Script.
Example: You can refer to Section 3.3.
4.3 a
Function parameters A represents the addition to the file. The format of its instructions are as follows:
[address1] A user entered by users
The following description is described in the above format:
Function parameter A is up to one address parameter.
The function parameter A follows "" "" "The character is used to indicate that the information entered by the user must be entered from the next row. If the information exceeds a line, it must be
The end of each row is added "".
The SED performing the addition action is as follows: When the data output in the Pattern Space, the SED follows the information entered by the output user.
example :
Title: Add "Multi-work System" after the line with the "UNIX" string. Suppose the contents of the Input.dat files are as follows:
Unix
Note: Use the function parameter a to add the input data to the data line containing the "UNIX" string.
The sed command is listed below:
Sed -e '/ unix / a
Multi-work system
'Input.dat
After performing the above command, its output is as follows:
Unix
Multi-work system
4.4 I
Function parameter i means inserting the data into the file. The format of its instructions are as follows:
[address1] i user-entered information
The following description is described in the above format:
Function parameter i is more than one address parameter.
The function parameter i is followed by "" The character is used to indicate the end, and the information entered by the user must be entered from the next row. If the information exceeds a line, it must be
The end of each row is added "".
The case where the SED performs insertion action is as follows: Before the data output in the Pattern Space, the SED will first output the information entered by the user.
example :
Title: Plug the "Central Research Institute" in the Central Research Institute in Input.dat, "Dean: Li Yuanzhe". Suppose INPUT.DAT files
By the following:
Dean: Li Yuanzhe
Explanation: Use the function parameter i to provide the information line "Article copyright belong to the Central Research Institute" inserted before the "Dean: Li Yuanzhe".
The sed command is listed below:
Sed -e '/ Dean: Li Yuanzhe / I
The copyright of the article belongs to the Central Research Institute
'Input.dat
The output after performing the above command is as follows:
The copyright of the article belongs to the Central Research Institute
Dean: Li Yuanzhe
4.5 C
The function parameter c represents the information in the file. The format is as follows:
[address1 [, address2]] C users entered by users
The following description is described in the above format:
The function parameter C is up to two bit address parameters.
The function parameter c is followed by "" "The character is used to indicate that the information entered by the user must be entered from the next row. If the information exceeds a line, it must be
The end of each row is added "".
SED performs changing the action: When the data output in the Pattern Space, the SED changes it into the information entered by the user. Example: Refer to the second, three of the sections 3.1.
4.6 P
The function parameter P represents the printed information. The format of its instructions are as follows:
[address1 [, address2]] P
The following description is described in the above format:
The function parameter P is up to two address parameters.
The situation in which the SED performs printing is as follows: SED is copied with a Pattern Space to the standard output file.
Example: Refer to the content starting with Section 3.4.
4.7 L
Function parameter L, in addition to the data in the data in the ASCII code, is the same as the function parameter P. For example, will
The ^ [Prints in the INPUT.DAT]
The Great ^ [Is A Movie Starring Steve McQueen.
After the command sed -e 'l' Input.dat, the output is as follows:
The Great