Sed - Command Function

xiaoxiao2021-03-06  18

1. #

In the script file, the text after the function parameter # is annotated. When the note is more than a multi-line, its line must be separated by "/".

2. !

Function parameters! Represents only the function parameters do not perform the specified address. Directive: [Address1 [, Address2]]! Function parameters

For example, delete, in addition to the "1996" string, all data lines, execute the command: SED -E '/ 1996 /! D' Input.dat

3. =

Function parameters = indicating the number of rows of the printing data. Directive: [address1, [address2]] =

Description: Function parameters = Up to two address parameters. When executed, the number of rows will output before the data output, that is, plus another row in front of each line, and the line number is displayed instead of directly adding the serial number.

Example: Printing the number of data in the input.dat file. Suppose the contents of Input.dat are as follows:

The UnixOperating system

With function parameters = to print the number of lines of data, the sed command is as follows: Sed -e '=' input.dat

4. A /

Function parameters A represents the addition to the file. Directive: [address1] A / user entered

Description: The function parameter A is up to one address parameter.

The function parameter A is followed by "/" characters to indicate this line, and the information entered by the user must be entered from the next row. If the information exceeds a line, you must add "/" at the end of each row.

The SED performing the addition action is as follows: When the data output in the Pattern Space, the SED follows the information entered by the output user.

Example: Topic: Add the "Multi-Task Operating System" after the data containing the "UNIX" string. Suppose the contents of the Input.dat files are as follows:

Unix

Note: Use a function parameter a to add the input data to the data line containing the "UNIX" string.

The SED command is listed as follows: SED -E '/ UNIX / A / Mission Operating System' Input.dat

5. I /

Function parameter i means inserting the data into the file. Directive: [address1] I / user input

Description: The function parameter i is more than one address parameter.

The function parameter i is followed by "/" characters to indicate that the line is over, the information entered by the user must be entered from the next row. If the information exceeds a line, you must add "/" at the end of each row.

The case where the SED performs insertion action is as follows: Before the data output in the Pattern Space, the SED will first output the information entered by the user.

example:

Insert the "Central Research Institute" in the Input.dat architecture "Dean: Li Yuanzhe" before. Suppose the contents of the input.dat are as follows: Dean: Li Yuanzhe

Explanation: Use the function parameter i to provide the information line "Article copyright belong to the Central Research Institute" inserted before the "Dean: Li Yuanzhe".

The SED command is listed as follows: Sed -e '/ Dean: Li Yuanzhe / I / article copyright belongs to the Central Research Institute' Input.dat

6. C /

The function parameter c represents the information in the file. Directive: [address1 [, address2]] C / user input

Description:

The function parameter c is up to two address parameters.

The function parameter c is immediately used to indicate that the information entered by the user must be entered from the next row. If the information exceeds a line, you must add "/" at the end of each row.

SED performs changing the action: When the data output in the Pattern Space, the SED changes it into the information entered by the user.

7. di

The function parameter D represents the information that deletes the mode space, starting the next loop. Directive: [Address1 [, Address2]] D

Description: The function parameter d is up to two address parameters.

SED execution delete action is as follows: Delete the information that meets the address parameters in Pattern Space, read the next information into the Pattern Space, and re-execute the Sed Script.

8. Di

The function parameter D represents the first line of information (including / n) in the Pattern Space. If there is still data in the mode space, the next loop is not read from the input data (nor output).

Directive: [Address1 [, Address2]] D) D description: The function parameter d is up to two address parameters.

The comparison of the function parameters D and D is as follows:

When there is only one data in Pattern Space, D is the same as D.

When there is a multi-line data in Pattern Space

D indicates that only the first line of information within Pattern Space; D is completely removed.

D indicates that after execution deletion, the next information is not added in the Pattern Space, and the remaining information will be re-executed Sed script; d, after reading the next line, execute the SED Script.

9. s

Function parameter s represents the Substitution file in the string. Directive: [Address1 [, Address2]] S / Pattern / Replacement / [FLAG]

Description: The function parameter S is up to two address parameters.

About "S / Pattern / Replacement / [FLAG]" has the following description:

Pattern: It is a Reguler Expression string. It indicates a string to be replaced in the file.

Replacement: It is a general string. But there is a special meaning of the following characters in it:

&: Represents preceded Pattern strings. For example: Sed -E 'S / Test / & my car /' data file name

In the command, & represents the Pattern string "test". Therefore, after execution, the "TEST" of the data file is replaced with "test my car".

/ N: The string that represents the n / (, /) in Pattern. For example: Sed -E 's // (TEST /) / (MY /) / (CAR /) / [/ 2/3/1] /' Data file name

In the instruction, / 1 indicates "Test", / 2 means "my", / 1 means "car" string. Therefore, after execution, the "Test My Car" of the data file is replaced with "[My Car Test]".

/: Available in some special symbols (such as the & and /) itself, or use it to represent the wrap.

Flag: Mainly used to control some replacement:

When the FLAG is G, the representative replaces all the strings that match (MATCH).

When the FLAG is a decimal number M, the Ran of the row is consistent with the row. When Flag is P, after replacing the first string that conforms to Pattern, the data outputs the standard output file.

When Flag is W WFILE, after replacing the first string that conforms to Pattern, it is output to the WFILE file (if WFILE does not exist, the file named WFile will be reopened).

When there is no FLAG, the first string in line with the Pattern in the data line is replaced with the Replacement string.

Delimiter: "/" in "/" / "" is used as a Delimiter. In addition to the blank, a newline, the user can use any character as Delimiter. For example, editing instructions: S # / usr # / usr1 # g

Use # to Delimiter in the above command. If you use "/" to do Delimiter, the SED will errors in Pattern and Replacement as Delimiter.

Example: Replace Input.dat file (there is no special specified, assume that the file file is set to input.dat) "1996" string "1997", and the data line is deposited in Year97.dat file: SED - E 'S / 1996/1997 / W Year97.dat' Input.dat

10. y

The function parameter y represents the character in the conversion data. Directive: [Address1 [, Address2] Y /ABC.../xyz.../

The function parameter y is a management bureau command, with two of the two address parameters, refuses to use the suffix flag / g, / abc ... / xyz ... / (x, y, z, a, b, c represent some characters) Argument, where ABC ... and XYZ ... must be the same. When converting, convert the A characters within the Pattern Space into the X character, the B character is converted to Y characters, and the C character is converted to z character ...

Example: Use the function parameter y to indicate the conversion of the SED to make alphabet size, the sed command is as follows: Sed -e 'y / abcdefghijklmnopqrstuvwxyz / abcdefghijklmnopqrStuvwxyz /' Input.dat

11. n

The function parameter n represents the next row of data. Directive: [Address1 [, Address2]]

Description: The function parameter n is up to two address parameters. The case is as follows: Output information in Pattern Space, read the next information to Pattern Space, and perform the next editing instruction.

Example: Output INPUT.DAT file internally couple data. Suppose the Input.dat file is as follows: TheunixOperationsystem

Description: Turn to instructions on the control power of the data output on the command column; use the function parameter N to replace the data line within the Pattern Space (odd line) in Pattern Space; use the function parameter P Data (even line) output in Pattern Space.

The SED command is listed as follows: Sed -n -e 'n' -e 'p' input.dat

12. N

The function parameter n indicates that the next row data is added to the Pattern Space, and it will automatically add "/ n" at the end of the original. Instruction: [address1 [, address2]] Description: The function parameter n is mostly matched with two address parameters. When the SED is executed, the next line of data is read and added to the Pattern Space, and the data line is separated by the Embedded Newline Character. In addition, when replacing, the wrap character can be used / n to match.

Example: Merge the following two lines of data. Suppose INPUT.DAT is as follows: The UnixOperating system

Note: First use the function parameter n to place the two lines of data in Pattern Space, and the separation number / n between the two rows of data is replaced with a blank interval, such that the two lines of data becomes a row output.

The SED command is listed as follows: SED -E 'N' -E 'S // N / /' Input.dat

13. p

The function parameter P represents the content printed in the current mode space. Directive: [address1 [, address2]] P

Description: The function parameter P is up to two address parameters. SED is copied with a Pattern Space content to a standard output file.

14. P

The function parameter P represents the information of the first line (the first newline letter / pre-first) in Pattern Space.

Directive: [address1, address2] P Description: The function parameter P is up to two address parameters.

P and P, in addition to the different data lines in the face-to-face Pattern Space, others are identical.

Example: Output INPUT.DAT file inside the odd number of lines. Suppose the contents of Input.dat are as follows: TheunixSystem

SED command column: Sed -n -e 'n' -e 'p' Input.dat

Note: On the command column, the control power of the data output will be transferred to the instruction. After the function parameter n is added to the Pattern Space, the first line of Pattern Space will be used to use the function parameter P. Old-numbered lines) Output, after the odd row output, the remaining data lines (even lines) in Pattern Space are abandoned, and finally, the entire output is only the original odd-numbered information.

15. l

Function parameter L, in addition to the NonPrinting Character in the data is listed in the ASCII code, which is the same as the function parameter P. For example, the ^ [is printed in the ASCII code) in the INPUT.DAT file: The great ^ [is a movie starring steve mcQueen.

Execute the command: Sed -e 'L' Input.dat

The output is as follows (the second line of data is the automatic output of the SED.):

The Great / 003 IS A MOVIE STARRING Steve McQueen.

The Great IS A Movie Starring Steve McQueen.

16. hide

The function parameter h indicates the data of the Pattern Space to Hold Space. Directive: [address1, [address2]]: Function parameter h with two address parameters, SED performs a temporary operation, covering the original information in Hold Space, when the SED is completed, HOLD Space is automatically cleared. 17. Hide

The only difference between the function parameter h and H is, and the SED is executed, the data cover the original information in OverWrite, and the h, the information is "Add (Append)" After the original information of Hold Space, it will be added Automatically add "/ N" at the end of the original.

18. G

Return the Hold Space in the Pattern Space (opposite to the function parameter H). The format of its instructions are as follows: [address1, address2] g

Function parameter g With two address parameters, the SED performs the original information in OverWrite Pattern Space.

19. G

The only difference between the function parameters G and G is that when the SED is executed, the data cover is covered in the original information in Pattern Space, and G, the information is "Add (Append) after the Pattern Space is the original information, will be added Automatically add "/ N" at the end of the original.

20. x

The function parameter X represents the information exchanged in Hold Space and Pattern Space. Instruction format: [address1 [, address2]] x

Function parameters X typically work with other function parameters that handle Hold Space.

For example, replace the first line of information within the INPUT.DAT file. At this time, the function parameter H is used to cooperate with X. Among them, the first data is stored in Hold Space in function parameter h; when the third line data appears in Pattern Space, the contents of the HOLD SPACE and Pattern Space are swapped in function parameter X. Thus, the third line of data is replaced by the first information. The command is as follows: Sed -e '1h' -e '3x' input.dat

twenty one. r

The function parameter r indicates that the content read into the file. Directive: [address1] r

Description: The function parameter r is up to one address parameter. In the instruction, the function parameter R is in the name of its file, can only have a space.

The case where the SED performs read operation is as follows: After the data output in the Pattern Space, the SED reads the contents of its files followed. When its file does not exist, the SED will do other instructions without any error messages.

twenty two. w

The function parameter w represents writing the file to its file. Directive: [address1 [, address2]] W Fold name

Description:

Function parameter w Multifies two address parameters.

In the instruction, the function parameter w is in the name of its file, can only have a space.

SED performs writing actions such as: Write the Pattern Space in its file. When the data is written, it will replace the data in the original file. Also, the SED will re-generate it when its file does not exist.

twenty three. : label

Function parameters ":" and function parameters "B" can establish a functionality similar to the GOTO directive in the SED Script. Among them, the function parameter ": label" establishes the position of the Script File inside the instructions.

twenty four. B label

Function parameter B Label jumps to the executable instruction to the end of the tag, if the tag is not saved, the branch is branched to the end of the script.

Function parameters: with B, the situation in the Script File is as follows

Editing instructions M1

:mark

Editing instruction M2

......

[address1, [address2]] B [记]

When the SED is executed to the command [Address1, [Address2] B [IdRess], if the information within the Pattern Space meets the address parameter, the SED will the next executed position branch to by: marker set by the mark, also Just is executed by "Editing Directive M2" ... In addition, if there is no mark in the function parameter b after the instruction, the SED will the next execution instruction BRANCH to Script File, which allows the SED Script to have a Case Statement structure in a C language in the SCRIPT. Example: Repeat 40 times of the starting letter in the Input.dat file. Suppose the contents of the Input.dat are as follows: A B C

Note: The transformer B P1 and: P1 constitute a loop (LOOP) that adds the letter, and simultaneously, when the letter occurs, the command B is used to jump out of the loop. The following is the first line of information in the document "a" as an example, describing how it adds 39 "A" to the same line continuously:

"A" is replaced with "AA" with instructions S / A / AA / (Reference Section 4.1).

With instructions B P1 and: P1 constitute a cycle (LOOP), it is an object to be repeatedly performed. Every time you execute a cycle, "A" on the data line will be one. For example, the first cycle data is turned "AA", and the second cycle is turned "AAA" ....

Use instructions [ABC] / {40 /} / b as conditions for stopping cycles. When the data line has a continuous 40 A appears, the function parameter B jumps to the end to stop the editing of this line.

Similarly, the other information line is also performed as described above.

The sed command is listed below:

Sed -E '{: P1 / A / S / AA / / B / S / B / B / / / C / S / C / CC / / [ABC] / {40 /} / bb p1}' INPUT. DAT

25. t lable

First execute a replacement editing instruction, if the replacement is successful, the editing instruction is jumped to: the Label is executed.

Basically, the function parameter T is similar to the function parameter B. In addition to the BRANCH executing T, there is a replacement of the preceding instruction before being executed. The situation within the Script file is as follows:

Editing instructions M1

:mark

Editing instruction M2

.

.

.

s /.../.../

[address1, [address2]] t [记]

Editing instruction M3

Where the function parameter B is in that the function parameter t branch is executed, the previous replacement instruction will be checked and not. If successful, BRANCH is executed; if it is not successful, the next editing instruction, such as the above editing instruction M3.

Example: Replace the data A1 in the input.dat file to C1, C1, replace it into a B1, B1 replacement to A1. The content of the input.dat file is as follows:

B1 A1 B1 C1 A1 C1

Description: All of the data lines in the input.dat file only need to perform a replacement action, but to avoid data replacing multiple times, so that the function parameter T forms a Case Statement structure in the SED Script, so that each line is replaced Once you can use the function parameter t to jump away from the replacement editor.

SED command column:

Sed -e '{

S / A1 / C1 /

t

S / C1 / B1 /

t

S / B1 / A1 /

t

} 'Input.dat

26. {}

A command with the same location parameters.

27. Qi

The function parameter Q represents the jump from the SED editing. Directive: [address1] q Description: Function parameter q Up to one address parameter. When the SED performs a skip operation, it stops entering the Pattern Space, and the data is stopped to the standard output file.

Example: Execute the editing instruction within the script_file, unless "Linux" string is encountered.

Note: Regardless of the instructions in Script_File, the user uses instruction / linux / q on the command column, and the function parameter Q will force the SED to do a jump action when "Linux".

The SED command is listed as follows: Sed -e '/ linux / q' -f script_file input.dat

转载请注明原文地址:https://www.9cbs.com/read-50717.html

New Post(0)