Shell sed command operation manual

Source: Internet
Author: User

Author: runming QQ: 226399587
Shell sed Manual
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 edit command in the order 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 execution of the edit command on the command is from left to right.
Generally, when there are few commands to edit, users usually execute them directly on the command. For example, delete 1 to 10 lines of data in yel. dat and change the "yellow" string in the remaining text to 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 the command's/yellow/black/G' (solution [6]) and 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 the regular expression string, indicating the row of the data to be edited. The function parameter function [argument] is a set function of sed, indicating the edited action to be executed. 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 is an example (the commands take the 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: the address is a 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 the Row m in the data file is deleted, the command is md.
Address: regular expression (refer to [appendix A]): When the data row contains strings that conform to the regular expression, the edit action indicated by the function parameter is executed. In addition, you must add "/" before regular expression "/". For example, if the command is/t. * t/d, all data lines containing two "t" letters are deleted. "." Indicates any character; "*" indicates that the first character can be duplicated any time. 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 parameter, it indicates that only the data row that matches the address is edited. When there are two address parameters, such as address1 and address2, it indicates editing the data area, and address1 indicates the starting data row, address2 indicates that the data row ends. The following examples are provided to describe the above content.
For example, if the command is d, all data rows in the file are deleted.
For example, if the command is 5d, it indicates that the fifth row of data in the file is deleted.
For example, if the command is 1,/apple/d indicates that the data area is deleted, from the first line in the file to the row containing the "apple" string.
For example, if the command is/apple/,/orange/d, it indicates that the data area is deleted, and the file contains 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 parameter function: 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 basic editing functions, the cooperation between address parameters and commands in instructions can also allow sed to complete 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, file name script_file) and use the option-f script_file, then let sed execute the edit command in script_file. The command 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 in which sed executes commands is still left-to-right in the Command column, for example, the commands in the file after-f is executed, it is 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 the "yellow" string in the white. dat, red. dat, and black. dat files into "blue". The command is as follows:
Sed-e's/yellow/blue/G' white. dat red. dat black. dat
When the preceding command is executed, sed depends on white. dat, red. dat, black. run the edit command s/yellow/blue/(refer to [section 4.1] to replace the string.
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" deliver the data from pattern space to the standard output file. However, with option-n, you can change the "Automatic" action of sed to the "passive" Edit Command executed by sed (solution [8]). to determine whether the results are output.
As shown above, option-n must work with the edit command; otherwise, the result cannot be obtained. For example, to print the data line containing the "white" string in the white. dat file, 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. Here, option-n moves the output control to the editing command;/white/p prints the data line containing the "white" string 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, it is very inefficient to use them for editing. This chapter provides examples to illustrate how to use sed to automatically execute these editing functions. In addition, in this chapter, the requirements of the document are described as follows:
In the file, execute... (Action)
The purpose is to quickly convert them into editing commands. In the "... documents" section, convert it to the address Parameter Representation in the instruction; in the "execute... Action" section, convert it to the function parameter representation. In addition, when "execute... when the 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 preceding three examples are as follows.
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 Save the edited data and use the function parameter w (refer to [section4.9]). move the file information to the file for storage, or use function parameter r (refer to [section4.8]) to move the file content to the file. Hold space is a temporary storage device used by sed to store data in pattern space. When sed executes function parameters h and H (refer to [section4.19, the pattern space data is saved to the hold space. When the number of parameters x, g, and G (refer to [section4.22]) are executed, the temporary data is obtained to the 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 pieces of data in the file are saved first (refer to [section4.19]) and deleted after hold space. The command 300G (refer to [section4.22]) indicates that, add the data in the hold space to the 300th data in the 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, this parameter is represented by the function parameter 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 indicates that when a blank row is added, the first blank row is deleted, and the remaining blank rows are re-executed. Execute the command again and delete a blank row. The result is reversed until the blank row is added as a non-blank row. Therefore, only one blank row is output at the end of the consecutive blank row.
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, output data rows containing the "gamma" string in the file. 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 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]), due to the case statement structure similar to that in C language formed by function parameter B, sed can process the data separately when the data contains the "omega" string; when the "omega" string is split into two rows, and the data does not contain the "omega" string. Next, we will divide sed script into the following three parts based on the above three situations. 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 parameter h (refer to [section 4.19]), which stores the first two rows in pattern space into hold space. Function parameter s/. * //, which indicates that the first and last two rows in the pattern space are combined ([11]) into one row. /Omega/B, which indicates that if the combined data contains a "omega" string, the data will be automatically output without executing the subsequent commands;
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 parameter 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. In this way, no matter the strings in the Data row or between lines can be completely searched.
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 | H | g | G | x | B | t |
In addition, in each section, we first briefly introduce the function parameters, then describe the format in which function parameters work with address parameters, and also describe how sed executes 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 with "[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 the wfile does not exist, the file named wfile will be re-opened ).
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 treat "/" in pattern and replacement as delimiter and an error will occur.
Example:
Question: replace input. dat file (if not specified, it is assumed that the file name is input. dat), and store the 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 store the replaced data row in year97.dat.
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 exists, you must add "" at the end of each row "".
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 exists, you must add "" at the end of each row "".
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 ": Li yuanzhe" document in the "input. dat" document. Assume that the content in the input. dat file 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 exists, you must add "" at the end of each row "".
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, the ^ [in the input. dat file below is printed in ASCII code.
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

Related Article

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.