Sed User Manual

Source: Internet
Author: User

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 to determine the edited object; use 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 that when the command column is executed, option-e must be added before it; while in the file (solution [4, you only need to add option-F before the file name. In addition, sed executes the editing commands in the order they are in the Command column or file.
 
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 Yel.
Dat contains 1 to 10 rows of data, and
Replace the "yellow" string in the remaining text with the "black" string. In this case, you can directly run the edit command
Command:
 
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 the 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,
Indicates the row of the data to be edited. The function parameter
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 notation is only the row of the data to be edited, using their lines or strings
To represent them. 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.
The following describes the expression of the address parameter in the instruction based on the content and number of the address parameter.
(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 row function parameter. 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,
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 conform to the regular expression, the function parameter indication is executed.
. In addition
"/" Must be added before and after regular expression "/". For example, if the command is/T. * t/d, it means to delete all
Two "T" data lines. Where ,"."
"*" Indicates that the first character can be any number of times. They are combined with ". *" to indicate two "T" letters.
Any string between them.
 
Number of address parameters: In the command, if there is no address parameter, it indicates that all data rows Execute function parameters.
Number indicates the editing action. If there is only one address
The parameter indicates that only the data row that matches the address is edited. When there are two address parameters, such
Address1, address2, indicates that the data area is executed
Edit. address1 indicates the start row and address2 indicates the end row. For the above content,
The following is an example.
 
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: Creates 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 data with the data entered by the user.
 
D. Delete the data.
D. Delete the first newline letter/prefix 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 a data row that is entered by the user.
N reads the next data.
N Add the next item to pattern space.
P: print the data.
P prints the first newline letter/prefix 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: Label
.
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 basic editing functions
The cooperation between commands can also make
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.
Stored in files
(For example, in the file name script_file), use the option-F script_file to execute SED
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, in the previous section,
It 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 in which sed executes commands is still
Left-to-right of a 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. Example
For example, replace
The "yellow" string in the white. dat, Red. dat, and black. DAT files is "blue ".
As follows:
Sed-E's/yellow/blue/G' white. dat red. dat black. dat
 
When the preceding command is executed, sed follows the order of White. dat, Red. dat, and black. dat.
Make S/yellow/blue/(refer to [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 set sed to "automatic"
"Action to" passive "by which it executes
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,
Print the white. dat file containing "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]) for control.
Output. Option-N controls the output.
Move to the editing command;/white/P prints the "white" string in the data line to the screen.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.