Sed manual-1. Introduction
Http://phi.sinica.edu.tw aspac@phi.sinica.edu.tw (08:05:00)
1. Introduction
Sed (Stream Editor) provides an editor that automates editing on Unix systems. You do not need to directly edit materials. Users can
Using the 20 different function parameters provided by SED, they are combined (solution [1]) to complete different editing actions. In addition, due to SED
Editing a file in a behavior Unit is also a line editor ).
Generally, sed is most commonly used to edit files that require constant overwriting of certain editing actions. For example, you can replace a string in a file with another word.
String. Compared with General UNIX editors (such as VI and Emacs), sed makes it easier to manually modify files.
The following sections describe:
When to use SED
Where to obtain SED
Sed can edit those actions
How sed works
1.1 when to use SED
When modifying a file, if you repeatedly repeat some editing actions, sed can automatically perform these editing actions at a time. For example
The sender name of the first email in the archive is "Tom" changed to "John". At this time, you only need to execute a simple sed command in the Command column.
Replace all "Tom" strings in the file with "John ".
Furthermore, when the file requires many different editing actions, sed can perform those different editing actions at a time. For example, sed can run
Delete all blank lines in the file, replace strings, and add text entered by the user to the sixth line of the file.
1.2 where to obtain SED
A general UNIX system is attached with sed. The SED versions attached to different UNIX systems are also different. If the Unix
If SED is not attached to the system, it can be obtained through anonymous FTP in the following places:
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 edit those actions
Sed can delete, change, append, insert, merge, or exchange data rows in a file, or read other
You can also replace the strings in the files or convert the letters in the files. For example
Delete blank rows in a row, replace "local" strings with "remote", "T" letters into "T", and combine 10th rows of data with 11th pieces of data
.
1.4 How sed works
Like other UNIX commands, sed reads the edited file from the standard input and sends the result from the standard output. Indicates that SED replaces the data line with "Unix"
Change to "Unix ",
In the figure above, standard input is the standard input, which is the place for reading data; standard output is the standard output, which is the place for sending results;
The two dotted boxes below the SED square in the middle represent the SED workflow. The dotted line on the left indicates that SED places the standard input data.
Pattern space. The dotted line on the right indicates that SED sends the edited data in pattern space to the standard output.
In the dotted box, the two solid boxes represent the pattern space and sed script respectively. Where, pattern space is a slow area, which is
Sed workplace, while sed script represents a group of executed editing commands.
In the figure, the dotted line "Unix" on the left is placed into the pattern space by the standard input. Then, in the dotted line on the right, sed executes the SED script
The Edit command S/Unix/(solution [2]) in, and the result "Unix" is replaced with "Unix", after which "Unix" is sent by pattern Space
Standard output.
In summary, when sed reads a row of data from the standard input and puts it into the pattern space, sed follows the SED script editing instructions
Edit a pair of documents in the pattern space. Then, the results in the pattern space are sent to the standard output, and then the next row is created.
Read. Re-execute the above action until all data rows are read.
Sed manual-2. Use SED
Http://phi.sinica.edu.tw aspac@phi.sinica.edu.tw (09:05:00)
Sed command columns can be divided into edit commands and file files. The editing command controls all edits. The file represents the processed file.
Sed editing commands are composed of address and function. During execution, sed uses its address parameter
Decides the edited object, and uses its function parameters (solution [3]) for editing.
In addition, the SED editing command can be executed in the Command column or in the file. The difference is only when the command column is executed.
Option-e must be added. In the file (solution [4]), you only need to add option-F before the file name. In addition, sed executes the edit command according to its
In the order of command columns or files.
The following sections describe how to execute edit commands, sed edit commands, execute edit commands in files, edit multiple files, and
Execute sed output control.
2.1 execute the edit command on the command Column
2.2 sed edit command
2.3 execute the edit command in the file
2.4 edit multiple files
2.5 execute sed Output Control
2. 1. Execute the edit command in the Command Column
When the edit command (refer to [section 2.2]) is executed on the command column, option-e must be added before it. The command format is as follows:
Sed-e' edit command 1'-e' edit command 2'... file File
All the edit commands are immediately following option-E and placed between two "'" special characters. In addition, the command is edited
Left and right.
Generally, when there are few commands to edit, users usually execute them directly on the command. For example, delete 1 to 10 rows of data in Yel. dat and
Replace the "yellow" string in the remaining text with the "black" string. In this case, you can run the edit command directly on the command. The command is as follows:
Sed-e '1, 10d '-E's/yellow/Black/G' Yel. dat
In the command, edit the command '1, 10d '(solution [5]) and delete lines 1 to 10; edit Command's/yellow/Black/G' (solution [6]),
Replace the "yellow" string (substuite) with the "black" string.
2.2 sed edit command
The format of the SED edit command is as follows:
[Address1 [, address2] function [argument]
The address parameters address1 and address2 are the number of rows or regular expression strings, indicating the row of the data to be edited. The function parameters
The number function [argument] is the SED internal function, which indicates the edited action.
The following two sections detail the representation of the address parameter and the function parameters available for selection.
2.2.1 representation of address parameters
In fact, the address Parameter Representation is only the row of the data to be edited, and they are represented by the number of lines or strings. The following are examples:
(The commands take function parameter D (refer to [section4.2]) as an example ):
If you delete the first row of data in the archive, the command is 10 days.
When you delete a row containing a "man" string, the command is/man/d.
If you delete rows from the first row to the second row, the command is 10th D.
Delete the data lines from the first row in the archive to the data line containing the "man" string. The command is 10,/man/d.
Next, let's take the content of the address parameter and its number as two points to fully describe the representation of the address parameter in the instruction (also take function parameter D as an example ).
Address parameter content:
Decimal number: This number indicates the number of rows. When the command is executed, the edit action indicated by the function parameter is executed for the data that matches the number of rows. For example,
If you delete the first row of data in the data file, the command is 15D (refer to [section4.2]). And so on. For example, if you delete Row M in a data file
Command is MD.
The address is regular expression (refer to [appendix A]):
When the data row contains strings that match the regular expression, the edit action indicated by the function parameter is executed. In addition
"/" Must be added before and after regular expression "/". For example, if the command is/T. * t/d, all data lines containing two "T" letters are deleted. Where ,"."
"*" Indicates that the first character can be any number of times. They are combined with ". *" to indicate any string between two "T" letters.
Number of address parameters: In the command, if there is no address parameter, it indicates that all data rows execute the edit action indicated by the function parameter; if there is only one address
When the parameter is set, only the data row that matches the address is edited. When there are two address parameters, such as address1 and address2, the parameter is executed for the data area.
Edit. address1 indicates the start row and address2 indicates the end row. The following examples are provided to describe the above content.
For example, the command is
D
It indicates deleting all data rows in the file.
For example, the command is
5D
It indicates deleting the fifth row of data in the file.
For example, the command is
1,/Apple/d
It indicates that the data area is deleted, from the first row in the file to the row containing the "apple" string.
For example, the command is
/Apple/,/orange/d
It indicates the deletion of the data area, from the file containing the "apple" string to the data line containing the "orange" String
2.2.2 function parameters
The following table describes the functions of all sed function parameters (refer to [Chapter 4.
Function Parameters
: Label is used to create a reference location for commands in the script file.
# Build a solution
The {} set contains commands with the same address parameters.
! Do not execute function parameters.
= Line number ).
A. Add the information entered by the user.
B label jumps the executed command to the reference location created.
C. Replace the information entered by the user.
D. Delete the data.
D. Delete the information before the first newline letter in the pattern space.
G. Copy data from hold space.
G. add data from hold space to pattern space.
H. Copy data from pattern space to hold space.
H. add data from pattern space to hold space.
L print the nonprinting character in data with ASCII code.
I. Insert the data row that the user enters.
N reads the next data.
N Add the next item to pattern space.
P: print the data.
P prints the information before the first newline letter in pattern space.
Q jumps out of SED editing.
R reads the content of the file.
S replacement string.
T label executes the replace edit command first. If it is replaced with ox P>, the edit command jumps to the label for execution.
W writes data to the archive.
X exchanges hold space and pattern space content.
Y conversion (Transform) characters.
Although sed only has the functions described in the table above that have the basic editing function, the cooperation between the address parameter and the instruction in the instruction can also enable
Sed completes most of the editing tasks.
2.3 execute the edit command in the file
When too many commands are executed, it is very confusing to write them on the command column. In this case, you can sort and store these commands in the file.
(For example, the file name is script_file). If option-F script_file is used, sed is allowed to execute the edit command in script_file. His life
The format is as follows:
Sed-F script_file File
The order of executing the edit command in script_file is from top to bottom. For example, the preceding example can be changed to the following command:
Sed-f ysb. scr Yel. dat
The YSB. scr file contains the following content:
1, 10 days
S/yellow/Black/g
In addition, options-E and-F can be mixed in the Command column. The order of SED command execution is still left to right in the Command column, such as after-F
Commands in the file are executed from top to bottom.
2.4 edit multiple file files
In the SED command column, multiple file files can be edited at a time, and they follow the editing command. For example, replace
In the white. dat, Red. dat, and black. DAT files, the "yellow" string is "blue". The command is as follows:
Sed-E's/yellow/blue/G' white. dat red. dat black. dat
When executing the preceding command, sed runs the edit command S/yellow/blue/in the order of White. dat, Red. dat, and black. dat (see [section 4.1].
String replacement.
2. 5. Control the execution output
Option-N (solution [7]) in the Command column indicates that the output is controlled by the editing command. According to the content in the previous chapter, sed will "automatically"
Pattern space is delivered to the standard output file. However, with option-N, you can change the "Automatic" action of sed to "passive" by which it performs
Edit the command (solution [8]) to determine whether the result is output.
As shown above, option-N must work with the edit command; otherwise, the result cannot be obtained. For example, the white. dat file contains "white"
The command is as follows:
Sed-n-e '/white/P' white. dat
In the preceding command, option-N works with the edit command/white/P (refer to [section4.6]) to control the output. Option-N controls the output.
Move to the editing command;/white/P prints the "white" string in the data line to the screen.
Sed manual-3. Example
Http://phi.sinica.edu.tw aspac@phi.sinica.edu.tw (13:05:00)
In the process of actually using the editor, you often need to replace the strings in the file, move, delete, and search for data rows.
Of course, the preceding functions can be achieved in a conventional Editor (such as VI and Emacs). However, once a file has a large number of such editing requirements
Editing with them is very inefficient. This chapter provides examples to illustrate how to use sed to automatically execute these editing functions. In addition, in this chapter
In this example, the file requirements are described as follows:
In the file, execute... (Action)
The purpose is to quickly convert them into editing commands. In the "... data" section, convert it to the address Parameter Representation in the instruction;
In the "execute... Action" section, it is converted into a function parameter. In addition, when the "execution... Action" is represented by several function parameters, you can
Use "{" and "}" to set these function parameters (solution [9]). The command format is as follows:
Address parameter {
Function parameter 1
Function parameter 2
Function parameter 3
.
:
}
The preceding command indicates that the operations indicated by function parameter 1, function parameter 2, and function parameter 3 are executed in sequence for information that meets the address parameter.
The following sections illustrate how sed replaces, moves, deletes, and searches for data.
3.1 Replace the information in the document
3.2 move documents
3.3 delete documents
3.4 search for documents
3.1 Replace the information in the document
Sed can replace the strings, data lines, and even data areas in the file. Where, it indicates that the function parameter in the command to replace the string is S (refer to [section4.1]);
The function parameter in the command to replace the data line or data area is C (refer to [section4.5]). The preceding three examples are as follows. The above situation
The following are three examples.
Example 1: Replace the "Phi" string in the data line containing the "machine" string in the file with the "Beta" string. The command column is as follows:
Sed-e '/machine/S/PHI/beta/G' input. dat (all subsequent file files are represented by input. dat)
Example 2: replace the 5th rows in the file with the sentence "those who in quarrels interpose, must often wipe a bloody nose .".
The command column is as follows:
Sed-e '5c
Those must often wipe a bloody nose.
'Input. dat
Example 3: replace the data area of rows 1 to 100 with the following two lines:
How are you?
Data be deleted!
The command column is as follows:
Sed-e '1, 100C
How are you?
Data be deleted!
'Input. dat
3.2 move documents
The user can use the hold space in sed to temporarily store and edit data, and use the function parameter W (refer to [section4.9]) to move the file data to the file for storage,
Or use the function parameter R (refer to [section4.8]) to move the file content to the file. Hold space is the temporary data stored by sed in pattern space.
Memory. When sed executes the function parameters H and H (refer to [section4.19]), it will save the pattern space data to the hold space. When the execution letter
When the number parameters X, G, and g (refer to [section4.22]), the temporary data is retrieved to pattern space. The following are three examples.
Example 1. Move the first 100 pieces of data in the file to the file 300th and then output the data. The command column is as follows:
Sed-F mov. scr file
MoV. scr file content is
1,100 {
H
D
}
300 GB
Where,
1,100 {
H
D
}
It indicates that the first 100 data in the file is saved first (refer to [section4.19]) and deleted after hold space; command 300g (refer to [section4.22])
Add the data in the hold space to the 300th data file and output the data.
Example 2: Move the data line containing the "Phi" string in the file to the Mach. inf file for storage. The command column is as follows:
Sed-e '/PHI/W mach. inf' file
Example 3: Move the content of the Mach. inf file to the data line containing the "Beta" string in the file. The command column is as follows:
Sed-e '/beta/R mach. inf' file
In addition, since SED is a stream (refer to [section1.4]) Editor, theoretically output file information cannot be moved back for editing.
3.3 delete documents
Because SED is a row Editor, sed can easily Delete individual data rows or the entire data area. Generally, function parameters d (refer to [section4.2]) or
D (refer to [section4.17. The following are two examples.
Delete all blank lines in the file. Its command Column
Sed-e '/^ $/d' File
Regular Expression (solution [appendix A]), ^ $ indicates a blank row. Here, the ^ limit must be the beginning of the row; $ limit must be the end of the row before the string.
Delete consecutive blank rows in the file and delete them as one row. Its command Column
Sed-e '/^ $ /{
N
/^ $/D
} 'File
The function parameter n (refer to [section4.16]) adds the next row of blank rows to pattern space. Function parameter/^ $/d,
When a blank row is added, the blank line in the first line is deleted, and the remaining blank lines are re-executed. Run the command again to delete a blank line,
Otherwise, only one blank row is output after the row is left blank.
3.4 search for documents
Sed can execute functions similar to the Unix Command grep. Theoretically, regular expression is available (refer to [appendix A]). For example
Row output with a "gamma" string. The command column is as follows:
Sed-n-e '/gamma/P' File
However, sed is a row editor, and its search is basically a row. Therefore, when some strings are split into two parts due to line breaks, the general method
That is, it is not feasible. In this case, you must search for these materials in a two-line manner. The following is an example:
For example, output data containing the "Omega" string in the file. The command column is as follows:
Sed-f gp. scr file
The content of the GP. scr file is as follows:
/Omega/B
N
H
S /.*//
/Omega/B
G
D
In the above sed script (solution [10]), the case statement structure similar to the C language is formed by function parameter B, so that SED can process
The data contains the "Omega" string. When the "Omega" string is split into two rows, and the data does not contain the "Omega" string. Then, based on the above three situations
Sed script is divided into the following three parts for discussion.
When the document contains "Omega", the editing command is executed.
/Omega/B
It indicates that when the data contains the "Omega" string, sed does not need to execute the following command on it, but directly output it.
If there is no "Omega" in the document, run the following edit command:
N
H
S /.*//
/Omega/B
The function parameter n (refer to [section 4.16]) indicates that the next row of data is read so that pattern space contains the first and last two rows of data. Function Parameters
Number H (refer to [section 4.19]), which means to store the first two rows of data in the pattern space into the hold space. Function parameter S/. * //, it
Combine the first and last two rows of data in the pattern space ([11]) into one row. /Omega/B, which indicates that if the combined information contains the word "Omega"
String, and the data is automatically output without executing the command after it;
When the combined information still does not contain "Omega", run the following edit command:
G
D
The function parameter G (refer to [section4.21]) indicates that the first two rows of data in the hold space are put back into the pattern space. Function Parameters
D (refer to [section4.17]), which indicates to delete the first row of data in the two rows, and re-Execute sed script for the remaining row of data. So,
Only strings in the data line or between lines can be searched completely.
Sed manual-4. Introduction to function parameters
Http://phi.sinica.edu.tw aspac@phi.sinica.edu.tw (07:00:00)
This chapter describes all function parameters provided by sed in the form of a function parameter.
| S | d | A | I | c | p | L | r | w | Y |! | N | q | = | # | n | d | p | H | G | x | B | T |
In addition, in each section, we first briefly introduce the function parameters, and then describe the format in which function parameters work with address parameters.
Sed execution of this function parameter.
4.1 s
Function parameter S indicates the replacement (substitute) string in the file. The command format is as follows:
[Address1 [, address2] S/pattern/replacemen/[flag]
The preceding formats are described as follows:
Function parameter S can work with up to two address parameters.
There are several notes about "s/pattern/replacement/[flag]" (solution [12:
Pattern: it is a regularexpression string. It indicates the string to be replaced in the file.
Replacement: it is a general string. However, the following characters have special significance:
&: Represents the previous pattern string. For example
Sed-E's/test/& my car/'document file name
Command, & indicates the pattern string "test ". After execution, the "test" of the document is replaced with "test my car ".
: Represents the string in pattern that is enclosed by N (,) (refer to [appendix. For example
Sed-E's/(test) (my) (CAR)/[2 3 1]/'profile name
In the command, 1 indicates "test", 2 indicates "my", and 1 indicates the "car" string. After execution, the "test my car" in the document is replaced.
To "[my car test]".
: Use it to restore the literal meaning of some special symbols (such as the & mentioned above), or use it to represent line breaks.
Flag: it is mainly used to control some replacement situations:
When the flag is g, all matching strings are replaced.
When the flag is a decimal digit m, it indicates replacing the M-th matching string in the row.
When the flag is P, it means to replace the first pattern-compliant string and output the data to the standard output file.
When the flag is W wfile, it means to replace the first pattern-compliant string and output it to the wfile file (if wfile does not exist, it will
Re-open the file named wfile ).
If no flag exists, replace the first pattern-compliant string in the data line with the replacement string.
Delimiter: In "/pattern/replace/[flag]", "/" is considered as a delimiter. In addition to blank and newline,
The user can use any character as the delimiter. For example, the following edit command
S #/usr #/usr1 # G
In the preceding command, verb | # | is delimiter. If "/" is used as delimiter, sed will "/"
An error occurs when it is used as a delimiter.
Example:
Question: Replace the input. dat file (if it is not specified at the end, it is assumed that the file name is input. dat) is "1996", and the file name is "1997 ".
Store these data rows in the year97.dat file.
Note: use function parameter s to indicate sed to replace "1996" string with "1997", and use flag W in s argument to indicate sed to replace
The data row is stored in the year97.dat file.
Sed command column:
Sed-E's/1996/1997/W year97.dat 'input. dat
4.2 d
The function parameter D indicates that the data row is deleted. The command format is as follows:
[Address1 [, address2] d
The preceding formats are described as follows:
Function parameter D can work with up to two address parameters.
Sed performs the following deletion actions:
Delete information that matches the address parameter in pattern space.
Read the next item into pattern space.
Execute sed script again.
For example, see Section 3.3.
4.3
Function parameter A indicates to add the data to the file. The command format is as follows:
[Address1] information entered by user
The preceding formats are described as follows:
Function parameter A can work with at most one address parameter.
Function parameter A is followed by the "" character to indicate the end of this line. The user input data must be input from the next line. If more than one row of data is required
Add "" to the end of each line "".
Sed: when the data in pattern space is output, sed follows the data input by the user.
Example:
Question: Add "Multi-job system" to the row containing "Unix" strings. Assume that the content of the Input. dat file is as follows:
UNIX
Note: function parameter A is used to add the input data to the row containing the "Unix" string.
The SED command column is as follows:
Sed-E'/Unix/
Multi-job system
'Input. dat
After executing the preceding command, the output result is as follows:
UNIX
Multi-job system
4.4 I
Function parameter I indicates to insert data into a file. The command format is as follows:
[Address1] information entered by the I user
The preceding formats are described as follows:
Function parameter I can work with at most one address parameter.
The function parameter I is followed by the "" character to indicate the end of this line. The user input data must be input from the next line. If more than one row of data is required
Add "" to the end of each line "".
Sed performs the insert action as follows: Before the data output in pattern space, sed outputs the data entered by the user.
Example:
Subject: The copyright of the article is before the "Central Research Institute" is inserted in the "input. dat" file to include "President: Li yuanzhe. Assume that the input. dat file is in
The content is as follows:
Dean: Li yuanzhe
NOTE: With function parameter I, the copyright of the document line is before the information line containing "President: Li yuanzhe" of the Central Research Institute.
The SED command column is as follows:
Sed-e '/Dean: Li yuanzhe/I
Article copyright belongs to the Central Research Institute
'Input. dat
The output after executing the preceding command is as follows:
Article copyright belongs to the Central Research Institute
Dean: Li yuanzhe
4.5 c
Function parameter C indicates changing the data in the file. The format is as follows:
[Address1 [, address2] C user input
The preceding formats are described as follows:
Function parameter C can work with up to two address parameters.
The function parameter C is followed by the "" character to indicate the end of this line. The user input data must be input from the next line. If more than one row of data is required
Add "" to the end of each line "".
Sed changes: when data is output in pattern space, sed changes the data that the user inputs.
Example 2 and 3 in section 3.1.
4.6 P
Function parameter P indicates that the data is printed. The command format is as follows:
[Address1 [, address2] P
The preceding formats are described as follows:
Function parameter P can work with up to two address parameters.
Sed copies the pattern space to the standard output file.
For example, see section 3.4.
4.7 L
Function parameter l, except that nonprinting character in the document can be listed in ASCII code, it is the same as function parameter P. For example
^ [Printed in ASCII code in the input. dat file
The great ^ [is a movie starring Steve McQueen.
After the command sed-e 'l' input. dat is executed, the output result is as follows:
The great