SED, AWK Learn Notes Part 2 ----- SED Editor

xiaoxiao2021-03-06  45

1.4 SED Editor SED is a non-interactive stream editor. It explains a script and performs the operations in the script. The SED is for flow, because of a lot of UNIX programs, its program input is input through input, and it is directly output to standard output. For example, sort is flowing, but VI is not. The input of the SED is often from one file or a pipe, but it can also be redirected from the keyboard. Outputs the screen that will be output by default, but can be captured in a file or send it to a pipe.

For SED usual usage:

1. Automatically edit one or more files 2. Simplify the repeatability operation of multiple files. 3. Write a conversion program

Sed is like running as follows: 1. Each row input is copied into the mode space, an intermediate buffer for editing operation 2. All editing commands in a SED script will be entered in order Each row is operated 3. The editing command is applied to all rows unless the processing range is limited. 4. If a command changes the input, then subsequent commands and address tests are applied to the current line, not the initial input line 5. Because the edit command changes a copy of the original input line, then the original input file Not changed. This copy will be sent to standard input (rather than redirecting to a file) 6. SED will maintain a retained space, and a separation buffer will be divided into subsequent read storage data.

1.4.1 The call of the command line syntax SED has the following two forms:

Sed [-n] [- e] 'Command' File (s) SED [-N] -f scriptfile file (s)

The first form allows you to specify an editing command, causing quotation marks. The second form allows you to specify a script file, a file that contains the sed command. These two forms will be used together, they may be used multiple times. If no file is specified, the SED is read from the standard input.

The following parameters are considered:

-n Off Default Output, the SED will only show rows that have a P command in the P command or the S command. -e cmd Next parameter is an editing command. It is useful if multiple scripts or multiple commands are used. -f File The next parameter is a script file that contains the editing command.

If the first row of the script is #n, the SED will be used by the -n parameter.

1.4.2 Syntax of the sed command

The SED command has a universal form: [address [, address]] [!] Command [arguments] SED copies the entered row to the mode space, the SED instruction consists of a series of addresses followed by the editing command. If the address of the command is the same as the row in the mode space, the command is executed on this line. If a command does not address, then it will be applied to each row. If a command changes the contents of the pattern space, then the subsequent commands apply to the current row of the pattern space, not the input line of the original file.

The address will be described in the next section. The command consists of a letter or a symbol, which will be described below and classified by alphabetical order. The parameters include the tags, R or W file names, and the replacement tag of the S command.

1.4.2.1 Mode Address A SED command can indicate 0, one or two addresses, one address can be a line number, symbol $ or a regular expression that is brought to a backschel. Regular expression is described in Section 1.3 See the Sed AWK learning note part1). In addition, N can be matched by any new row (caused by n commands), but is not a new line of the end of the mode space.

If the command is specified: 0 addresses, the command is applied to all input lines. If the command is specified: 1 address, then the command is applied to any matching address line. Some commands receive only one address, such as: A, I, R, Q, and =. If the command is specified: two addresses separated by commas, then the command is applied to the first matching row and after all of the parallel package The line of the two addresses. If the command is specified: a follow-up! The address is applied to all mismatched rows. 1.4.2.2 Example Commands: S / XX / YY / G function: Alternative on all rows.

Command: / BSD / D function: Delete all banks with BSD

Command: / ^ begin /, / ^ END / P function: Print the content between Begin and End, including these two lines.

Command: / save /! D Function: Delete all rows that do not contain SAVE

Command: / begin /, / End /! S / XX / YY / G function: Remove the part of Begin and End, do a replacement on all rows.

Brackets (()) are used in SED to embide an address to another address or to apply multiple commands for the same address for matching.

[/ Pattern / [, / Pattern /]] {Command1command2} The bracers must end this line, and the brass must be self-use. Please make sure there is no space behind the parentheses.

1.4.3 Summary of the sed command

In the following list, the SED command is simply described in a function classification. Complete description, packages grammar and examples can be found in the following 1.4.4.

1.4.3.1 Basic editing commands:

A After a line, add text C to replace text (often a text block) i before inserting text D delete row S to replace Y conversion characters (TR in Unix)

1.4.3.2 Row Information = Display line number 1 Display the control P display this line with the ASCII code display

1.4.3.3 Input Output Process N Skip this line to the next row R Read the content from another file to the output stream W Write the input line to another file Q to exit the SED script (no other output).

1.4.3.4 Pull and place H Copy to the reserved space, cover the original content. H Copy to the reserved space, add to the original content, G retrieve the reserved space, eliminate the target line G to retrieve the reserved space, add to the X swap retention space and mode space in the pattern space in the mode space.

1.4.3.5 Branch Command B Branch to the end of the flag or script T is known to the b command, but only after the replacement is replaced. : Label marked by T or B branch

1.4.3.6 Multi-line input processing n Reads another input line (create an embedded new line) SD delete embedding row P print embedded line

1.4.4 Summary summary of the sed command

#: Start comments in a SED script. It is only valid only in the first character. (Some versions are allowed to be annotated anywhere, but it is best not to rely on this.) If a script is the first line of a script, then the SED will behave like the -N parameter is used.

: label: Tag a row in the script is used to swap control by b or t command. Mark up to 7 characters can be allowed

=: [/ Pattrern /] = to write out each line of pattern identification to the standard output stream

A: [Address] A text Add the following text in the matching address. If the text exceeds a line, then the new line must be hidden, which is guided by a backlash. The text is ended from the first new row that is not hidden in such a hidden way. The text is not valid in the mode space, and the subsequent command cannot be applied to it. The result of this command is sent to the standard output stream at the end of the editing command, and does not consider what happened to the current row in the mode space.

B: [Address1 [, address2]] B [label] unconditionally transfer to the other of the script: Label tag, that is, the next command after the Label is the next command to the current line application. If no tag is specified, the control jumps to the script, so that there is no other command can be applied to the current line. C: [address1 [, address2] ctext replaces the line selected by the text address. When a range of rows are specified, all rows are replaced with a group or a separate text copy. The content of the pattern space is deleted, the subsequent editing command is applied to the mode space (or text)

D: [address1 [, address2]] deletes the address row (or more) from the mode space. In this way, this line will not be transferred to the standard output, and a new line of entries is read, and the editing is restored to the first command of the script.

D: [address1 [, address2]] D Deletes the plurality of lines of multi-line generation generated by n commands in the mode space, and restore the first line of the script. If this command clears the pattern space, the new line entered is Read, just like the D command is executed.

g: [address1 [, address2]] Paste the content of the reserved space back (see the H and h commands) into the mode space, clear the content before the mode space

G: [address1 [, address2] g is the same as G, and the difference is that a new line and reserved space are appended to the mode space instead of overwrite. H: [address1 [, address2] h puts the content in the mode space Return the reserved space, a special temporary buffer. The previous content in the retaining space is deleted. You can use the h command to save it before editing this line.

H: [address1 [, address2] h plus a new row and plus the pattern space to the reserved space. Even if you keep the space is empty H, H is still adding a new line, h is like an incremental copy.

I: [address] IText inserts text before the address matching

l: [Address1 [, address2]] lists the contents in the mode space, display non-print characters with the ASCII code. The length will be folded.

N: [address1 [, addresw2]] n from the next row to the read mode space. The current row is sent to the standard output, and the next line becomes the current line. Control Go to the back of the N command instead of recovery to the top of the script

N: [address1 [, address2]] Add the next input line to the mode space; the new line is separated from the previous content of the mode space. (This command is to allow mode matching between two lines) by use n To match a new line of embedded, you can match the mode in many rows.

P: [address1 [, address2]] p Print address line. Note that this can cause repeated output unless the default output is turned off by the #n or -n command number parameter. It is often used to change stream control (D, B, N) before command, which prevents current rows from being output.

P: [address1 [, address2]] P Prints the first part of the multi-line mode space generated by n commands. When N is not applied to this, it is the same as p

Q: [Address] Q exits when an address is encountered. The address of the address is first written to the output with any text added by the following A or R command (if the default output is not turned off),

R: [address] R file reads the contents of the file and adds to the back of the mode space. There must be only one space between the R and file names

s: [address1 [, addres2]] S / PAT / REPL / [FLAGS] replaced the REPL in the address line. If the mode address is used, the mode // represents the mode specified by the last address. Any hair cyset is applied. Used to exit the delimiter in PAT or REPL. The following tags can be declared: N: Replace the Nth instance of the PAT on each address line. N can be any number from 1 to 512, default is 1 g: replace all instances of PAT in each address line, not just the first instance P: If the success is replaced, print this number, if multiple replacement If successful, the SED will print a number of copies of multiple numbers. W: file If a replace is executed, write this line to a file, up to 10 files T: [address1 [, address2]] t [label] testing replacement on the address line can not be successfully executed If you can, jump to: Label. If the Label is not declared, the control will jump to the bottom of the script. The T command is just like a CASE in the C language or all other shell languages. You test this case, if it is endless, you quit this structure

W: [address1 [, address2]] w: [Address1] adds the content of the mode space to a file, if the command is running instead of the mode space, this operation is executed. There is only one space after W and file names. Up to 10 different files can be opened. This command generates a file when the file does not exist. If the file exists, its content will be overwritten by each script. Multiple write commands to one file will add content to the end of the file

x: [address1 [, address2]] X: X: X: Switches the mode space with the content of the reserved space.

Y: [address1 [, address2] y / abc / xyz / transmit character. Turn a to x, b to Y, C to Z, etc.

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

New Post(0)