Sed (1) SED (1)
Name SED - Flow Editor Summary: SED [-N] script [file ...]
Sed [-n] [-e script] ... [-f script_file] ... [file ...]
Description: SED is edited according to the script (up to 100 commands), then the default is the standard input), then copy the edited content to the standard output. Sed is row based, execute commands in each line, if one The last line of the file is not over, the last line of the file will be ignored. Option: SED can identify the following options: -f script_file will execute the script file named script_file as script. Years Edited according to the script If there is only one-E option and no -f option, -e can omit. -N prohibits the default output operation. Sed sequentially explains all-E script and -f script parameters according to a given order, and pay attention to: mix When the -E and -F options, you should consider clear the order of execution to avoid incorrect or undesirable results. Script: A SED script consists of a lot of editing commands, editing commands, the form is as follows: [Address [, Address ]] Function [arguments]
Under normal circumstances, each line to pattern space entered by the SED loop copy (referred to as PS unless there is other content after the D command), then the following commands match the addresses, after the script is completed, copy the PS content copy To standard output (except for using the -n option) and then delete the PS content.
Sometimes, in order to access data in the previous PS backward, you can save all or part of the PS content by using the retention space (referred to as HS).
Command Address: Address or the decimal number of marked line numbers, $ represents the last line; or the context address, that is, the / regular expression / match line.
Context Expression Address, Structure /? Regular Expression? And / regular expression / equivalence, where it can be any of the characters. Note: In the case of the context, the second X represents in / xabc / xdefx itself, so regular The expression is Abcxdef. Escape sequence / n matches a new line in the mode space.
Fix Symbol (.) Match any of the characters except the last line of charm mode space. No address is valid for each mode space. (Each row is valid) only one address command only matches the match The line of the address is edited a command with two addresses, match the first address to the second address (if the second address is a number and less than or equal to the first address), only match The first line) SED supports basic regular expression syntax.
The editorial command can also use the negative function (!) To indicate the corresponding command. Command function: In the list below, the maximum number of addresses per function allowed in parentheses, the other related content involved is as follows:
TEXT one or more lines, if a row is finally ended with a backslash (/), representing a line. The backslash is treated as a backslash in the s command. RFile must be at the end of the command line, and the front must There is a space. WFile must be on the tail of the command line, and you must have a space .WFile is created before processing. Up to 10 different WFILE parameters. SED identification:
(1) A / Text Add, insert the text into the current line in the mode space, then output.
(2) B Label jumps to: the tag specified by the command, if the label is not specified, jump to the end of the script. (2) C / text modification, the representation of 0 or 1 or 2 addresses At the end, the Text content is placed until the next round of loop is started. (The current row in the mode space is replaced, and return the header execution.) (2) D Delete the content in the Pattern Space (possibly or row), and returns to the head of the script execution. (2) D delete content before / n in the first pattern space, and returns execution script head
(2) G Directly replaced the contents of the PS directly in the HS (2) G to attach the contents of the HOLD SPACE to the back of the Pattern Space (additional, automatically add one / n at the end of the original line / n) (2) h PS content Save to HS
(2) H Place a newline in the content of the HS, and follow the contents of the PS
(1) I / text is inserted into the text content after the match is then output.
(2) L lists the PS content in the standard output, and the characters that cannot be printed are output in the form of three octal numbers in the reverse line, and the Too long guild will automatically wrap. (2) n Copy the PS content to the standard output ( If the command line mode -N option or in the script file #n command is not used, read the next (next line) information to the PS.
(2) n Add Next (next line) information to PS, N read only one line, use a loop to call multiple N to read multiple lines. If n encounters EOF, SED exits (current line number change) (2) P prints PS content. (2) P prints the first / N before the PS. (1) Q Jump to the end of the script no longer starts the loop.
(1) R RFILE reads the contents of the RFILE and outputs the RFILE content before reading the next input line.
(2) S / REGULAR EXPRESSION / REPLACEMENT / FLAGS replaces the regular expression match in the PS, which can be used in addition to the slash (/) any character. More comprehensive description, see Ed (1). Flags 0 or the following value:
N n = 1-2048 (line_max). Alternative operations only in the nth appearance. G global replacement, replace all the appearances of matching parts. If replacement occurs and the default output is turned off (with -n or #n , Then print the PS content,
W WFILE If a replacement occurs, write the PS content into the file wfile. (2) The t label t command After reading a line or last T command, if there is a S command to perform success, then the t command jumps, otherwise Jump; if Label is empty, jump to the end of the script. (2) W WFILE adds the PS content to the file wfile. (2) X swap HS and PS content. (2) Y / STRING1 / STRING2 / Transformation: Transformation The relationship is a change in the position of the character List of the two strings. The length of the two strings must be equal. (2)! Function negation. Decorate the function or group (if the function is {) indicates that the address matching is not Perform a corresponding function. (0): Label defines a B or T command to jump tags.
(1) = The line number of the current row is output as a row to the standard output. (2) {} pair, a plurality of commands enclosed as a command group, perform these commands in order when a mode space matches The syntax is as follows: {cmd1 cmd2 cmd3..} (0) empty command is ignored.
(0) # If the # symbol appears in the lead, the entire line is a comment. There is an exception: if the character is N indicated that the default output is turned off, # n will also be ignored. A script file must be at least There is a non-invasive row.
External impact: Environment variable lang provides default values when the international variable is not set or empty: "c", if any international variable contains an invalid setting, SED will default all international variables " C ". (This external variable applies to all SD commands other than install-sd. Determine the language used by the message. If not specified as an empty string, use the default value" c ". Show SD proxy And the language used by the daemon log message is set by the system configuration variable script /etc/rc.config.d/lang. For example, / etc / rc.config.d / lang must be set to "LANG = JA_JP.SJIS" or " LANG = JA_JP.EUCJP "to use Japanese to display proxy and daemon log messages. You can also use Export Lang = command. For more information, see the LANG (5) Online Help page.) LC_ALL determines the language environment, used to override LANG Any value of the language environment category specified in all environment variables starting in LC_
LC_CTYPE determines how to interpret the byte sequence of text data as characters (such as vendor-defined attributes using single-byte characters or multi-byte characters). LC_MESSAGES determines the language of writing messages NLSPATH to determine LC_MESSAGES to handle the processing message catalog of LC_MESSAGES Location International Character Set Support: Supports Single- and Multi-Byte character sets.
Example: Replace ABC in a file with XYZ SED 'S / ABC / XYZ /' File1> File1.out implementation above the same function, but use shell or environment variable value var1 and var2 sed "when finding and replacing strings / $ VAR1 / $ VAR2 / "File1> file1.out or sed 's /' $ var1 '/' $ var2 '/' file1> file1.out single command implementation multiple replacement, ABC replaces XYZ, LMN is replaced with RST Sed -E 's / abc / xyz /' -e 's / lmn / rst /' file1> file1.out or
Sed -e 's / abc / xyz /' / -e 's / lmn / RST /' / file1> file1.out warning: The total number of commands in the SED cannot exceed 100, HS maximum 8192 characters. SED only processed Text file. Author sed Was developed by OSF and HP.
See Also awk (1), ED (1), GREP (1), Environ (5), LANG (5), Regexp (5).
Sed: a Non-Interactive Streaming Editor Tutorial in The Text Processing Uses Guide.
Standards Conformance Sed: Svid2, Svid3, XPG2, XPG3, XPG4, POSIX.2
Hewlett-Packard Company - 6 - HP-UX 11i Version 2: August 2003