Original link:
http://phi.sinica.edu.tw/aspac/reports/96/96005/
SED Manual
Institute of Mathematics, Academia Sinica
ASPAC Project
aspac@phi.sinica.edu.tw
Technical Report: 96005
December 1, 1996
Version:1.0
--------------------------------------------------------------------------------
Table of Contents:
Copyright Notice
1. Introduction
When to Use sed
Where to Get sed
What sed Can Do
How sed Works
Using sed
Executing Edit Commands on the Command Line
sed's Edit Commands
Address Parameter Notation
Function Parameters
Executing Edit Commands in a File
Editing Multiple Files
Controlling Output
Examples
Substituting Data in Files
Moving Data in Files
Deleting Data in Files
Searching for Data in Files
Introducing Function Parameters
s
d
a
i
c
p
l
r
w
y
!
n
q
=
#
N
D
P
h
H
g
G
x
b
t
Appendix A: Common Regular Expressions
Appendix B: Acceptance of Special Characters in Regular Expressions by sed in HP-UX Release 9.01 and SunOS 5.4
References
Notes
--------------------------------------------------------------------------------
Introduction
--------------------------------------------------------------------------------
1.Introduction
Sed (Stream EDitor) is an editor on UNIX systems that automates the editing work, allowing users not to directly edit the data. Users can use more than 20 different function parameters provided by sed to combine (Note ) them to complete different editing actions. In addition, since sed edits files line by line, it is also a line editor.
Generally, sed is most commonly used to edit files that require repeating certain editing actions continuously, such as replacing a certain string in a file with another string. Compared with general UNIX editors (interactive ones like vi, emacs) that modify files manually, using sed is more labor-saving. The following sections will introduce respectively:
When to Use sed
Where to Get sed
What sed Can Do
How sed Works
1.1 When to Use sed
When modifying a file, if you repeatedly perform certain editing actions, you can use sed to automatically perform these editing actions at once. For example, to change the sender's alias "Tom" to "John" in 1000 emails in the received file, you can simply execute a simple sed command on the command line to replace all "Tom" strings in the file with "John".
Moreover, when a file requires many different editing actions, sed can perform those different editing actions at once. For example, sed can delete all blank lines in a file at once, replace strings, and add text entered by the user to the sixth line of the file, etc.
1.2 Where to Get sed
Generally, the sed is attached to the general UNIX system itself. The versions of sed attached to different UNIX systems are also different. If the sed is not attached to the UNIX system you are using, you can obtain it through anonymous ftp to 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 What sed Can Do
sed can delete (delete), change (change), append (append), insert (insert), merge, exchange data lines in a file, or read data from other files into the file, and can also substitute (substuite) strings in them, or convert (tranfer) letters in them, etc. For example, delete consecutive blank lines in a file into one line, replace the string "local" with "remote", convert the letter "t" to "T", merge the data of line 10 and line 11, etc.
1.4 How sed Works
Like other UNIX commands, sed reads the edited file from standard input and sends the result to standard output. The following figure shows that sed replaces the data line "Unix" with "UNIX",

In the figure, the upper standard input is the standard input, which is the place to read data; the standard output is the place to send the result; the two dashed squares below the middle sed box represent the workflow of sed. Among them, the left dashed square means that sed puts the standard input data into the pattern space, and the right dashed square means that sed sends the data after editing in the pattern space to the standard output.
In the dashed square, the two solid squares respectively represent the pattern space and the sed script. Among them, the pattern space is a buffer, which is the working place of sed; and the sed script represents a set of editing instructions to be executed.
In the figure, the "Unix" on the left dashed square is put into the pattern space from the standard input; then, in the right dashed square, sed executes the editing instruction s/Unix/UNIX/ in the sed script (Note ), the result "Unix" is replaced with "UNIX", and then "UNIX" is sent from the pattern space to the standard output.
In summary, when sed reads a line of data from the standard input and puts it into the pattern space, sed executes the editing on the data in the pattern space one by one according to the editing instructions of the sed script, and then sends the result in the pattern space to the standard output, and then reads the next line of data. This action is repeated until all data lines are read.
--------------------------------------------------------------------------------
Using sed
--------------------------------------------------------------------------------
Using sed
The sed command line can be divided into edit commands and file parts. Among them, the edit commands are responsible for controlling all editing work; the file part represents the file to be processed. The sed edit commands are composed of two parts: address and function. When executing, sed uses its address parameter to determine the object of editing; and uses its function parameter (Note ) to edit.
In addition, the sed edit commands can be executed not only on the command line but also in a file. The difference is that when executing on the command line, the option -e must be added before it; when in a file (Note ), only the option -f needs to be added before its file name. In addition, sed executes the edit commands in the order they are on the command line or in the file.
The following sections will introduce executing edit commands on the command line, sed edit commands, executing edit commands in a file, editing multiple files, and controlling sed output.
2.1 Executing Edit Commands on the Command Line
2.2 sed's Edit Commands
2.3 Executing Edit Commands in a File
2.4 Editing Multiple Files
2.5 Controlling sed Output
2.1. Executing Edit Commands on the Command Line
When the edit command (refer to ) is executed on the command line, the option -e must be added before it. The command format is as follows:
sed -e 'edit command 1' -e 'edit command 2' ... file
Among them, all edit commands are immediately after the option -e and are placed between two " ' " special characters. In addition, the execution of the edit commands on the command line is from left to right.
When there are not many edit commands, users usually execute them directly on the command line. For example, to delete the data from line 1 to 10 in yel.dat and replace the string "yellow" with "black" in the remaining text. At this time, the edit commands can be executed directly on the command line, and the command is as follows:
sed -e '1,10d' -e 's/yellow/black/g' yel.dat
In the command, the edit command '1,10d' (Note ) deletes the data from line 1 to 10; the edit command 's/yellow/black/g' (Note ) replaces the string "yellow" with "black".
2.2 sed's Edit Commands
The format of the sed edit command is as follows:
]function
Among them, the address parameters address1 and address2 are line numbers or regular expression strings, representing the data lines to be edited; the function parameter function is the built-in function of sed, representing the editing action to be executed.
The following two sections will carefully introduce the notation of the address parameter and which function parameters are available for selection.
2.2.1 Address Parameter Notation
In fact, the address parameter notation is just to represent the data lines to be edited by their line numbers or strings in them. The following examples are used to illustrate (the command uses the function parameter d (refer to ) as an example):
To delete the data in line 10 of the file, the command is 10d.
To delete the data line containing the string "man", the command is /man/d.
To delete the data from line 10 to line 200 in the file, the command is 10,200d.
To delete from line 10 to the data line containing the string "man" in the file, the command is 10,/man/d.
Next, according to the content and number of address parameters, the notation of the address parameter in the command is fully explained (also taking the function parameter d as an example).
Content of address parameters:
The address is a decimal number: this number represents the line number. When the command is executed, the editing action indicated by the function parameter is executed on the data that matches this line number. For example, to delete the data in line 15 of the data file, the command is 15d (refer to ). And so on, for example, to delete the data in line m of the data file, the command is md.
Address is a regular expression (refer to ):
When there is a string in the data line that matches the regular expression, the editing action indicated by the function parameter is executed. In addition, "/" must be added before and after the regular expression. For example, the command is /t.*t/d, which means deleting all data lines containing two "t" letters. Among them, "." represents any character; "*" represents that the previous character can be repeated any number of times, and they are combined with ".*" to represent any string between two "t" letters.
Number of address parameters: In the command, when there is no address parameter, it means that all data lines execute the editing indicated by the function parameter; when there is only one address parameter, it means that only the data line that matches the address is edited; when there are two address parameters, such as address1,address2, it means editing the data area, where address1 represents the starting data line and address2 represents the ending data line. For the above content, the following examples are used for specific explanation.
For example, the command is
d
It means deleting all data lines in the file.
For example, the command is
5d
It means deleting the data in line 5 of the file.
For example, the command is
1,/apple/d
It means deleting the data area from the first line of the file to the data line containing the string "apple".
For example, the command is
/apple/,/orange/d
It means deleting the data area from the data line containing the string "apple" to the data line containing the string "orange" in the file
2.2.2 What Function Parameters Are Available
The following table introduces the functions of all sed function parameters (refer to ).
Function Parameter Function
: label Establish a position for mutual reference of instructions in the script file.
# Establish a comment
{ } Collect instructions with the same address parameter.
! Do not execute the function parameter.
= Print the line number (line number) of the data.
a\ Add data entered by the user.
b label Branch the executed instruction to the reference position established by :.
c\ Replace data with data entered by the user.
d Delete data.
D Delete the data before the first newline character \ in the pattern space.
g Copy data from the hold space.
G Add data from the hold space to the pattern space.
h Copy data from the pattern space to the hold space.
H Add data from the pattern space to the hold space.
l Print nonprinting characters in the l data in ASCII code.
i\ Insert and add the data line entered by the user.
n Read the next data.
N Add the next data to the pattern space.
p Print data.
P Print the data before the first newline character \ in the pattern space.
q Exit the sed edit.
r Read the content of another file.
s Substitute strings.
t label First execute a substitution edit command, and if the substitution is successful, jump the edit command to : label to execute.
w Write data to another file.
x Swap the contents of the hold space and the pattern space.
y Transform characters.
Although sed only has the above-mentioned few basic function parameters with editing functions, through the cooperation between the address parameters in the instruction and between instructions, sed can also complete most editing tasks.
2.3 Executing Edit Commands in a File
When there are too many commands to be executed and it is very messy to write on the command line, you can sort and store these commands in a file (for example, the file name is script_file), and use the option -f script_file to let sed execute the edit commands in script_file. The command format is as follows:
sed -f script_file file
Among them, the order of executing the edit commands in script_file is from top to bottom. For example, the example in the previous section can be changed to the following command:
sed -f ysb.scr yel.dat
Among them, the content of ysb.scr is as follows:
1,10d
s/yellow/black/g
In addition, on the command line, options -e and -f can be mixed, and the order of sed executing commands is still from left to right on the command line. When executing to the edit commands in the file after -f, it is executed from top to bottom.
2.4 Editing Multiple Files
In the sed command line, multiple files can be edited at one time, and they follow after the edit commands. For example, to replace the string "yellow" with "blue" in the files white.dat, red.dat, and black.dat, the command is as follows:
sed -e 's/yellow/blue/g' white.dat red.dat black.dat
When the above command is executed, sed executes the edit command s/yellow/blue/ (refer to to replace the string in order of white.dat, red.dat, black.dat.
2.5. Controlling Output
The option -n (Note ) on the command line means that the output is controlled by the edit command. From the content of the previous chapter, it is known that sed will "automatically" send data from the pattern space to the standard output file. But with the option -n, sed can change this "automatic" action to "passive" to be determined by the edit commands it executes (Note ) whether the result is output.
It can be seen from the above that the option -n must be used together with the edit command, otherwise the result cannot be obtained. For example, to print the data line containing the string "white" in the white.dat file, the command is as follows:
sed -n -e '/white/p' white.dat
In the above command, the option -n and the edit command /white/p (refer to ) cooperate together to control the output. Among them, the option -n transfers the output control to the edit command; /white/p prints the data line containing the string "white" on the screen.
--------------------------------------------------------------------------------
3. Examples
--------------------------------------------------------------------------------
3. Examples
Generally, in the process of actually using the editor, it is often necessary to perform actions such as substituting strings in files, moving, deleting, and searching for data lines. Of course, general interactive editors (such as vi, emacs) can all do the above functions, but when there are a large number of the above editing requirements for a file, using them to edit is very inefficient. This chapter will use examples to illustrate how to use sed to automatically perform these editing functions. In addition, in the examples of this chapter, the requirements of the file are described in the following way:
Replace ... data in the file with ... (action)
In this way, the purpose is to quickly convert them into edit commands. Among them, the part of "... data" is converted into the address parameter representation in the instruction; the part of "execute ... action" is converted into the function parameter representation. In addition, when "execute ... action" is to be represented by several function parameters, these function parameters can be collected by using "{ " and " }" (Note ). The instruction form is as follows:
address parameter{
function parameter 1
function parameter 2
function parameter 3
.
:
}
The above instruction means that for the data that matches the address parameter, the actions represented by function parameter 1, function parameter 2, function parameter 3... are executed in sequence. The following sections respectively give examples to illustrate the commands of sed for substituting data, moving, deleting data, and searching for data.
3.1 Substituting Data in Files
3.2 Moving Data in Files
3.3 Deleting Data in Files
3.4 Searching for Data in Files
3.1 Substituting Data in Files
Sed can substitute strings, data lines, and even data areas in files. Among them, the function parameter s (refer to ) in the instruction representing substituting strings; the function parameter c (refer to ) in the instruction representing substituting data lines or data areas. The above situations are illustrated by the following three examples. The above situations are illustrated by the following three examples.
Example 1. Replace the string "phi" in the data line containing the string "machine" in the file with the string "beta". The command line is as follows:
sed -e '/machine/s/phi/beta/g' input.dat (from now on, the file name is input.dat)
Example 2. Replace the data in line 5 of the file with the sentence "Those who in quarrels interpose, must often wipe a bloody nose.". The command line is as follows
sed -e '5c\
Those must often wipe a bloody nose.
' input.dat
Example 3. Replace the data area from line 1 to 100 in the file with the following two lines of data:
How are you?
data be deleted!
Then the command line is as follows
sed -e '1,100c\
How are you?\
data be deleted!
' input.dat
3.2 Moving Data in Files
Users can use the hold space in sed to temporarily store the data being edited, use the function parameter w (refer to ) to move the file data to another file for storage, or use the function parameter r (refer to ) to move the content of another file to the file. The hold space is a register used by sed to temporarily store the data in the pattern space. When sed executes the function parameters h and H (refer to ), it will temporarily store the pattern space data in the hold space; when executing the function parameters x, g, G (refer to ), it will take the temporarily stored data to the pattern space. The following three examples are used to illustrate.
Example 1. Move the first 100 data in the file to the output after line 300 in the file. The command line is as follows:
sed -f mov.scr file
The content of mov.scr is
1,100{
H
d
}
300G
Among them,
1,100{
H
d
}
It means that the first 100 data in the file are first stored (refer to ) in the hold space and then deleted; the instruction 300G (refer to ) means that the data in the hold space is added after the data in line 300 of the file and output.
Example 2. Move the data line containing the string "phi" in the file to be stored in the mach.inf file. The command line 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 string "beta" in the file. The command line is as follows:
sed -e '/beta/r mach.inf' file
In addition, since sed is a stream (refer to ) editor, theoretically, the data of the output file cannot be moved back for editing.
3.3 Deleting Data in Files
Because sed is a line editor, sed can easily delete individual data lines or entire data areas. Generally, the function parameters d (refer to ) or D (refer to ) are used to represent. The following two examples are used to illustrate.
Delete all blank lines in the file. The command line is
sed -e '/^$/d' file
Regular expression (Note ), ^$ means a blank line. Among them, ^ restricts that the following string must be at the beginning of the line; $ restricts that the preceding string must be at the end of the line.
Delete consecutive blank lines in the file and delete them to become one line. The command line is
sed -e '/^$/{
N
/^$/D
}' file
Among them, the function parameter N (refer to ) means that the data line below the blank line is added to the pattern space. The function parameter /^$/D means that when the added one is a blank line, the first blank line is deleted, and the remaining blank lines are then re-executed once. The instruction is re-executed once, and a blank line is deleted. This is repeated until the blank line is followed by a non-blank line, so that only one blank line remains after consecutive blank lines and is output.
3.4 Searching for Data in Files
Sed can perform functions similar to the UNIX command grep. In theory, regular expressions (refer to ) can be used. For example, to output the data line containing the string "gamma" in the file. Then the command line is as follows:
sed -n -e '/gamma/p' file
However, sed is a line editor, and its search is basically line-based. Therefore, when some strings are split into two parts due to line breaks, the general method is not feasible. At this time, the data must be searched by merging two lines. The situation is as follows in the following example:
Example. Output the data containing the string "omega" in the file. The command line is as follows
sed -f gp.scr file
The content of gp.scr is as follows:
/omega/b
N
h
s/.*\n//
/omega/b
g
D
In the above sed script (Note ), because the function parameter b forms a case statement structure similar to C language, sed can respectively handle the situations where the data contains the string "omega"; when the string "omega" is split into two lines; and when the data does not contain the string "omega". Next, according to the above three situations, the sed script is divided into the following three parts for discussion.
When the data contains "omega", execute the edit command
/omega/b
It means that when the data contains the string "omega", sed does not need to execute the following instructions on it anymore, and directly outputs it.
When the data does not contain "omega", execute the following edit commands
N
h
s/.*\n//
/omega/b
Among them, the function parameter N (refer to ) means that the next line of data is read so that the pattern space contains the previous and next two lines of data. The function parameter h (refer to ) means that the previous and next two lines of data in the pattern space are stored in the hold space. The function parameter s/.*\n// means that the previous and next two lines of data in the pattern space are merged (Note ) into one line. /omega/b means that if the merged data contains the string "omega", then the following instructions are not executed anymore, and this data is automatically output;
When the merged data still does not contain "omega", execute the following edit commands
g
D
Among them, the function parameter g (refer to ) means that the two lines of data before merging in the hold space are put back into the pattern space. The function parameter D (refer to ) means that the first line of data in the two lines of data is deleted, and the remaining line of data is made to re-execute the sed script. In this way, the strings in the data line or between lines can be searched completely.
--------------------------------------------------------------------------------
Introducing Function Parameters
--------------------------------------------------------------------------------
Introducing Function Parameters
This chapter will introduce all the function parameters provided by sed in the way of one function parameter per section, 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 of the function parameter is briefly introduced first, and then the format of the function parameter cooperating with the address parameter is explained, and the working situation of sed executing this function parameter is also described.
4.1 s
The function parameter s represents substituting (substitute) strings in the file. The instruction format is as follows:
] s/pattern/replacemen/
The following points are explained for the above format:
The function parameter s cooperates with up to two address parameters.
Regarding "s/pattern/replacement/" (Note ), the following points are explained:
pattern: it is a regular expression string. It represents the string to be replaced in the file.
replacement: it is a general string. But the following characters have special meanings:
&: represents the previous pattern string. For example
sed -e 's/test/& my car/' file name
In the instruction, & represents the pattern string "test". Therefore, after execution, "test" in the data file is replaced with "test my car".
\n: represents the string enclosed by the nth \( and \) (refer to ) in the pattern. For example
sed -e 's/\(test\) \(my\) \(car\)//' file name
In the instruction, \1 represents "test", \2 represents "my", and \1 represents "car" string. Therefore, after execution, "test my car" in the data file is replaced with "".
\: It can be used to restore the literal meaning of some special symbols (such as & and \) above, or to represent a line break.
flag: mainly used to control some substitution situations:
When flag is g, it means replacing all matching (match) strings.
When flag is the decimal number m, it means replacing the mth matching string in the line.
When flag is p, it means that after replacing the first matching pattern string, the data is output to the standard output file.
When flag is w wfile, it means that after replacing the first matching pattern string, it is output to the wfile file (if wfile does not exist, a file named wfile will be re-opened).
When there is no flag, the first matching pattern string in the data line is replaced with the replacement string.
delimiter: In "/pattern/replace/ ", "/" is used as a delimiter. In addition to blank (blank) and newline (newline), users can use any character as the delimiter. For example, the following edit command
s#/usr#/usr1#g
In the above command, \verb|#| is the delimiter. If "/" is used as the delimiter, sed will treat "/" in the pattern and replacement as the delimiter and an error will occur.
Example:
Topic: Replace the string "1996" in the input.dat file (if not specified specially later, it is assumed that the file name is input.dat) with "1997", and store the data lines that have been replaced in the year97.dat file.
Description: Use the function parameter s to instruct sed to replace the string "1996" with "1997", and use the flag w in the s argument to instruct sed to store the replaced data lines in the year97.dat file.
sed command line:
sed -e 's/1996/1997/w year97.dat' input.dat
4.2 d
The function parameter d means deleting the data line, and the instruction format is as follows:
] d
The following points are explained for the above format:
The function parameter d cooperates with up to two address parameters.
The situation when sed executes the delete action is as follows:
Delete the data in the pattern space that matches the address parameter.
Read the next data into the pattern space.
Re-execute the sed script.
Example: Refer to section 3.3.
4.3 a
The function parameter a means adding data to the file. The instruction format is as follows:
a\ data entered by the user
The following points are explained for the above format:
The function parameter a cooperates with up to one address parameter.
The function parameter a is followed by the "\" character to indicate the end of this line, and the data entered by the user must be entered from the next line. If the data exceeds one line, "\" must be added at the end of each line.
The situation when sed executes the add action is as follows: After the data in the pattern space is output, sed then outputs the data entered by the user.
Example:
Topic: Add "Multitasking System" after the data line containing the string "UNIX". Assume that the content of input.dat is as follows:
UNIX
Description: Use the function parameter a to add the input data after the data line containing the string "UNIX".
The sed command line is as follows:
sed -e '/UNIX/a\
Multitasking System
' input.dat
After executing the above command, the output result is as follows:
UNIX
Multitasking System
4.4 i
The function parameter i means inserting data into the file. The instruction format is as follows:
i\ data entered by the user
The following points are explained for the above format:
The function parameter i cooperates with up to one address parameter.
The function parameter i is followed by the "\" character to indicate the end of this line, and the data entered by the user must be entered from the next line. If the data exceeds one line, "\" must be added at the end of each line.
The situation when sed executes the insert action is as follows: Before the data in the pattern space is output, sed first outputs the data entered by the user.
Example:
Topic: Insert "The copyright of the article belongs to the Academia Sinica" before the data line containing "Director: Lee Yuan-tseh" in the input.dat file. Assume that the content of input.dat is as follows:
Director: Lee Yuan-tseh
Description: Use the function parameter i to insert the data line "The copyright of the article belongs to the Academia Sinica" before the data line containing "Director: Lee Yuan-tseh".
The sed command line is as follows:
sed -e '/Director: Lee Yuan-tseh/i\
The copyright of the article belongs to the Academia Sinica
' input.dat
After executing the above command, the output is as follows:
The copyright of the article belongs to the Academia Sinica
Director: Lee Yuan-tseh
4.5 c
The function parameter c means changing the data in the file. The format is as follows:
]c\ data entered by the user
The following points are explained for the above format:
The function parameter c cooperates with up to two address parameters.
The function parameter c is followed by the "\" character to indicate the end of this line, and the data entered by the user must be entered from the next line. If the data exceeds one line, "\" must be added at the end of each line.
The situation when sed executes the change action: When the data in the pattern space is output, sed changes it to the data entered by the user.
Example: Refer to Example 2 and 3 in section 3.1.
4.6 p
The function parameter p means printing data. The instruction format is as follows:
] p
The following points are explained for the above format:
The function parameter p cooperates with up to two address parameters.
The situation when sed executes the print action is as follows: sed copies a copy of the content of the pattern space to the standard output file.
Example: Refer to the content at the beginning of section 3.4.
4.7 l
The function parameter l, in addition to listing the nonprinting characters in the data in ASCII code, is the same as the function parameter p. For example, print the ^
.
4.8 r
The function parameter r means reading the content of another file into the file. The instruction format is as follows:
r file name
The following points are explained for the above format:
The function parameter r cooperates with up to one address parameter.
In the instruction, there can only be one space between the function parameter r and the file name.
The situation when sed executes the read action is as follows: After the data in the pattern space is output, sed reads the content of the other file and outputs it. When the other file does not exist, sed still executes other instructions without any error message.
Example: Refer to Example 3 in section 3.1.
4.9 w
The function parameter w means writing the file to another file. The instruction format is as follows:
] w file name
The following points are explained for the above format:
The function parameter w cooperates with up to two address parameters.
In the instruction, there can only be one space between the function parameter w and the file name.
The situation when sed executes the write action is as follows: Write the data in the pattern space to another file. When writing data, it will overwrite (overwrite) the data in the original file. In addition, when the other file does not exist, sed will recreate (creat) it.
Example: Refer to Example 2 in section 3.1.
4.10 y
The function parameter y means converting characters in the data. The instruction format is as follows:
]y /xyz.../abc.../
The following points are explained for the above format:
The function parameter cooperates with up to two address parameters.
In the instruction, /abc.../xyz.../ (x, y, z, a, b, c represent certain characters) is the argument of y. Among them, the number of characters in abc... and xyz... must be the same.
When sed executes the conversion, the a character in the data in the pattern space is converted to the x character, the b character is converted to the y character, the c character is converted to the z character, etc.
Example:
Topic: Convert the lowercase letters in the input.dat file to uppercase. Assume that the content of input.dat is as follows:
Sodd's Second Law:
Sooner or later, the worst possible set of
circumstances is bound to occur.
Description: Use the function parameter y to instruct sed to convert the case of letters.
The sed command line is as follows:
sed -e '
y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
' input.dat
After executing the above command, the output result is as follows:
SODD'S SECOND LAW:
SOONER OR LATER, THE WORST POSSIBLE SET OF
CIRCUMSTANCES IS BOUND TO OCCUR.
4.11 !
The function parameter ! means not executing the function parameter. When there is the following instruction,
] ! function parameter
It means that the data that matches the address parameter does not execute the function parameter. For example, delete all data lines except those containing the string "1996", then execute the following command
sed -e '/1996/!d' input.dat
4.12 n
The function parameter n means reading the next line of data. The instruction format is as follows:
] n
The following points are explained for the above format:
The function parameter n cooperates with up to two address parameters.
The situation when sed executes the action of reading the next line is as follows:
Output the data in the pattern space.
Read the next data into the pattern space.
Execute the next edit command.
Example (can be compared with the example in ):
Topic: Output the even-numbered line data in the input.dat file. Assume that the content of input.dat is as follows:
The
UNIX
Operation
System
Description: On the command line
With the option -n, transfer the output control right (refer to ) to the instruction.
Use the function parameter n to replace the data line (odd-numbered line) in the pattern space with the next line of data (even-numbered line).
Use the function parameter p to output the data (even-numbered line) in the pattern space.
Finally, the output only has the original even-numbered line data.
The sed command line is as follows:
sed -n -e 'n' -e 'p' infro.dat
After executing the above command, the output result is as follows:
UNIX
System
4.13 q
The function parameter q means jumping out of sed. The instruction format is as follows:
q
The following points are explained for the above format:
The function parameter q cooperates with up to one address parameter.
When sed executes the jump action, it stops inputting the pattern space data and stops sending the data to the standard output file.
Example:
Topic: Execute the edit commands in script_file on the file, unless the string "Linux" is encountered.
Description: No matter what instructions are in script_file, the user only needs to use the instruction /Linux/q on the command line, and the function parameter q will force sed to jump out when encountering "Linux".
The sed command line is as follows:
sed -e '/Linux/q' -f script_file input.dat
4.14 =
The function parameter = means printing the line number of the data. The instruction format is as follows:
] =
The following points are explained for the above format:
The function parameter = cooperates with up to two address parameters.
When executed, the line number will be output before the data is output.
Example:
Topic: Print the line number of the data in the input.dat file. Assume that the content of input.dat is as follows:
The UNIX
Operating System
Description: Use the function parameter = to print the line number of the data.
The sed command line is as follows:
sed -e '=' input.dat
After executing the above command, the output result is as follows:
1
The UNIX
2
Operating System
4.15 #
In the script file, the text after the function parameter # is a comment. When the comment text exceeds multiple lines, the line breaks must be separated by the "\" line break character.
4.16 N
The function parameter N means adding the next data in the pattern space. The instruction format is as follows:
] N
The following points are explained for the above format:
The function parameter N cooperates with up to two address parameters.
When sed executes, the next line of data is read and added to the pattern space, and the data lines are separated by the embedded newline character. In addition, when substituting, the newline character can be matched with \n.
Example:
Topic: Merge the following two lines of data. Assume that the content of input.dat is as follows:
The UNIX
Operating System
Description: First use the function parameter N to place the two lines of data in the pattern space, and then use the function parameter s/\n/ / to replace the separator \n between the two lines of data with a space, so that the two lines of data become one line output.
The sed command line is as follows:
sed -e 'N' -e 's/\n/ /' input.dat
After executing the above command, the output result is as follows:
The UNIX Operating System
4.17 D
The function parameter D means deleting the first line of data in the pattern space. The instruction format is as follows:
D
The following points are explained for the above format:
The function parameter D cooperates with up to two address parameters.
The comparison between the function parameter D and d is as follows:
When there is only one line of data in the pattern space, D and d have the same effect.
When there are multiple lines of data in the pattern space
D means only deleting the first line of data in the pattern space; d deletes all.
D means that after execution, the pattern space does not add the next data, and the remaining data is re-executed the sed script; d reads the next line and then executes the sed script.
Example: Refer to the second example in section 3.3.
4.18 P
The function parameter P means printing the first line of data in the pattern space. The instruction format is as follows:
P
The following points are explained for the above format:
The function parameter P cooperates with up to two address parameters.
P is the same as p except that the number of data lines in the facing pattern space is different.
Example (can be compared with the example in ):
Topic: Output the odd-numbered line data in the input.dat file. Assume that the content of input.dat is as follows:
The
UNIX
System
Description: On the command line
With the option -n, transfer the output control right (refer to ) to the instruction.
Use the function parameter N to add the even-numbered line to the odd-numbered line in the pattern space.
Use the function parameter P to output the first line (odd-numbered line) in the pattern space.
After the odd-numbered line is output, the remaining data line (even-numbered line) in the pattern space is abandoned and not output. Finally, the output only has the original odd-numbered line data.
The sed command line is:
sed -n -e 'N' -e 'P' infro.dat
After executing the above command, the output result is as follows:
The
System
4.19 h
The function parameter h means temporarily storing the data of the pattern space in the hold space. The instruction format is as follows:
] h
The following points are explained for the above format:
The function parameter h cooperates with up to two address parameters.
When sed executes the temporary storage action, it will overwrite (overwrite) the original data in the hold space.
When sed executes all, the data in the hold space will be automatically cleared.
Example: Refer to the example in section 3.4.
4.20 H
The only difference between the function parameter H and h is that when sed executes h, the data overwrites (overwrite) the original data in the hold space, while H, the data is "appended (append)" after the original data in the hold space. For the example, please refer to Example 1 in section 3.2.
4.21 g
The function parameter g means the opposite action of the function parameter h, which means putting the data in the hold space back into the pattern space. The instruction format is as follows:
g
The function parameter g cooperates with up to two address parameters.
When sed executes the back action, the data overwrites (overwrite) (Note ) the original data in the pattern space.
Example: Refer to the example in section 3.4.
4.22 G
The only difference between the function parameter G and g is that when sed executes g, the data overwrites (overwrite) the original data in the pattern space, while G, the data is "appended (append)" after the original data in the pattern space. For an example, please refer to Example 1 in section 3.2.
4.23 x
The function parameter x means exchanging the data in the hold space and the pattern space. The instruction format is as follows:
] x
The function parameter x mostly cooperates with other function parameters that process the hold space. For example, replace the data in line 1 of the input.dat file with the data in line 3. At this time, use the function parameters h and x to cooperate. Among them, use the function parameter h to store the first data in the hold space; when the data in line 3 appears in the pattern space, use the function parameter x to exchange the contents of the hold space and the pattern space. In this way, the data in line 3 is replaced by the first data. The command line is as follows:
sed -e '1h' -e '3x' input.dat
4.24 b、:label
The function parameter : and the function parameter b can establish a function similar to the GOTO instruction in the BASIC language in the sed script. Among them, the function parameter : establishes a mark; the function parameter b branches the next executed instruction to the mark for execution. The cooperation between the function parameter : and b in the script file is as follows
.
.
.
Edit command m1
:Mark
Edit command m2
.
.
.
]b
Among them, when sed executes to the instruction ]b , if the data in the pattern space matches the address parameter, sed will branch the next executed position to the mark set by :Mark (Note ), that is, execute from "Edit command m2" again. In addition, if there is no mark after the function parameter b in the instruction, sed will branch the next executed instruction to the end of the script file. Using this can make the sed script have a case statement structure similar to C language.
Example:
Topic: Repeat the first letter of the data line in the input.dat file 40 times. Assume that the content of input.dat is as follows:
A
B
C
Description: Use the instructions b p1 and :p1 to form a loop (loop) to execute the action of increasing letters, and at the same time, when 40 letters appear, use the instruction b to jump out of the loop. The following takes the first line of data "A" in the file as an example to describe how it continuously adds 39 more "A"s in the same line:
Use the instruction s/A/AA/ (refer to section4.1) to replace "A" with "AA".
Use the instructions b p1 and :p1 to form a loop (loop), which aims to repeatedly execute the above action. Each time the loop is executed, the "A" on the data line will be one more. For example, the data line becomes "AA" in the first loop, and becomes "AAA" in the second loop...
Use the instruction \{40\}/b (Note ) as the condition to stop the loop. When there are 40 consecutive A's appearing in the data line, the function parameter b will jump the executed instruction to the end and stop editing this line.
Similarly, the same way is executed for other data lines.
The sed command line is as follows:
sed -e '{
:p1
/A/s/A/AA/
/B/s/B/BB/
/C/s/C/CC/
/\{40\}/b
b p1
}' input.dat
4.25 t
Basically, the function parameter t is similar to the function parameter b in function, except that before executing the branch of t, it will first test whether the previous substitution instruction has been successfully substituted. The situation in the script file is as follows:
.
.
.
Edit command m1
:Mark
Edit command m2
.
.
.
s/.../.../
]t
Edit command m3
Among them, the difference from the function parameter b is that when executing the function parameter t branch, it will first check whether the previous substitution instruction is successful. If it is successful, execute the branch; if it is not successful, do not branch, and continue to execute the next edit command, such as the above edit command m3.
Example:
Topic: Replace A1 with C1, C1 with B1, and B1 with A1 in the input.dat file. The content of input.dat is as follows:
Code
B1
A1
B1
C1
A1
C1
Description: All data lines in the input.dat file only need to execute a substitution action, but to avoid the data being substituted multiple times, the function parameter t is used in the sed script to form a case statement structure similar to C language, so that each line of data can immediately jump out of the substitution edit after being substituted once.
The sed command line is:
sed -e '{
s/A1/C1/
t
s/C1/B1/
t
s/B1/A1/
t
}' input.dat
--------------------------------------------------------------------------------
Common Regular Expressions
--------------------------------------------------------------------------------
Common Regular Expressions
Ordinary characters The regular expression composed of ordinary characters has the same meaning as the literal meaning of the original string.
^string Restrict that the string must appear at the beginning of the line.
$string Restrict that the string must appear at the end of the line.
. Represents any character.
Character set, used to represent any one of all characters between the two brackets, such as represents any character other than all characters between the two brackets.
-& The character set can use "&" to specify the range of characters.
* Used to describe that the previous character (or character set) can be repeated any number of times.
\n Represents the embedded new line character (imbedded new line character).
\(...\) Use "\(" "\)" to enclose a part of the regular expression in the regular expression; later, "\1" can be used to represent the first part enclosed by "\(" "\)". If the regular expression uses "\(" "\)" several times to enclose different parts, then use "\1", "\2", "\3",... (up to "\9") in turn.
In addition, on different platforms, there are some different restrictions on regular expressions. For details, refer to appendix B.
--------------------------------------------------------------------------------
Notes
--------------------------------------------------------------------------------
Notes
Note 1.
It is the sed script that will be mentioned later.
Note 2.
The instruction s/Unix/UNIX/ means replacing "Unix" with "UNIX". Please refer to section 4.1.
Note 3.
There are more than 20 function parameters available for selection in the instruction.
Note 4.
This file is called the script file later.
Note 5.
In the edit command 1,10d, the address parameter is 1,10, so the data from line 1 to 10 executes the delete action specified by the function parameter d.
Note 6.
In the edit command s/yellow/black/g, since there is no address parameter, all data lines must execute the replacement action specified by the function parameter s/yellow/black/g. In the function parameter s/yellow/black/g, /yellow/black/g is the argument of s, which means replacing all "yellow" in the data line with "black".
Note 7.
The command format is as follows:
sed -n .. ..
Note 8.
These editing instructions may be one of p, l, s.
Note 9.
In some cases, the edit command can also be used instead of the function parameter. For example, Example 2 in section3.3.
Note 10.
Here, the sed script refers to the content of the gp.scr file. It means the edit command executed by sed this time.
Note 11.
This function parameter means replacing (removing) the newline mark between the two lines in the pattern space. Therefore, there is only one line of data in the pattern space.
Note 12.
/pattern/replacement/ is the argument of the function parameter s.
Note 13.
Note that at this time, although the data is put back into the pattern space, the content of the hold space remains unchanged.
Note 14.
Note that there must be no space between ":" and the mark.
Note 15.
The address parameter \{40\} means 40 A letters or 40 B letters or 40 C letters. Among them, means "A" or "B" or "C"; the following \{40\} means that there are 40 of the previous letters. For regular expressions, please refer to
Appendix A
.
--------------------------------------------------------------------------------
SED Manual
References
--------------------------------------------------------------------------------
References
``SED - A Non-interactive Text Editor '' Lee E.McMahon , AT&T Bell Laboratories Murray Hill,New Jersey 07947.
`` sed & awk'' Dale Dougherty , O'Reilly & Associates , Inc.1990.
``SunOs5.1 Editing Text Files'',Sun Microsystem,Inc.1992.
``HP 9000 computers -- Text Processing : User Guide'',Hewlett-Packard Company.1991.
`` Ye Daye. Introduction to SED, a Tool for Automatically Editing Files'', Bulletin of the Computing Center, Academia Sinica, Volume 12, Issue 2.