SED command Learning

Source: Internet
Author: User

Thread: The following is only a few records of my study of SED, welcome to make Bricks

Sed basic Edit command:
# Create annotations (\ for line breaks)
{} Sets instructions with the same address parameter.
! Do not execute function parameter (if match is not performed)
Note @:$! n #不是最后一行, execute n command
Sed '/---/!s/--/replace/g ' #注意使用技巧
= Print line number for matching line--up to one address parameter (number of rows in front of data line output)
A\ adding data entered by the consumer--up to one address parameter (\ means line break, indispensable)
I\ Insert a data row to add a consumer input--up to one address parameter (\ means line break, indispensable)
Note @: The insert and append commands do not affect the contents of the schema space, while the added content is transparent to subsequent commands
When the command finishes executing, the added content will be printed
Note @: If you add more than one line, you must use the \ Mask newline character, otherwise it will be increased by the first line break termination
The insert command can be used to put a blank line before the current line
Append and change commands can be used to place a blank line after the current line
Insert or append after each line that is matched to
C\ replaces matched content with data entered by the user-up to two address parameters (\ For line breaks, indispensable)
Note @: When there are two address parameters, replace all rows that are addressed with the target string (the target string is output only once)
When a change command is enclosed in curly braces as one of a set of commands and is used in a range of rows, the target string is output multiple times
This command will clear the pattern space (the same effect as the Delete command)
d Delete data--up to two address parameters
Note @: Deletes the current row in the pattern space and reads the next line into the pattern space
Empty even if there are multiple lines in the pattern space
Commands that change the control flow
L print output (with P and image), where nonprinting character (nonprinting characters) are expressed in ASCII code
Note @: Longer lines are broken
n outputs the current line and reads the next input line into the pattern space (overwriting the data in the original pattern space)--up to two address parameters
Note @: Transfer control to the command after N instead of reverting to the top of the script
The next line of input replaces the forward of the pattern space
P Print matching line-up to two address parameters
Note @: May cause duplication of output unless you use-n/#n command-line option restrictions
Often used to change the flow control (d, N, b) before the command may prevent the current row from being output
Q exits when a matching row is encountered and prints a matching line (unless the default output is limited)--up to one address parameter
Note @: Includes content appended with the A/R command
There are functions like how many rows the head reads
R reads the contents of another file and appends to the schema space content--up to one address parameter (there must be a space between r and the file)
Note @: Reads the contents of the file after the determined line into the schema space
File does not exist, will not error
The output of the read command cannot be suppressed
W Append the contents of the schema space to the file-up to two address parameters (there must be a space between W and the file)
Note @: This action occurs when a command is encountered, rather than in the output mode space content
Up to 10 files can be opened per script
If the file exists, the contents will be changed each time the script is executed (overwrite or append to be confirmed)
Append Write
s replacement string--maximum of two address parameters
Note @:sed ' [address [, Address]] S/pattern-to-find/replacement-pattern/[g p w N] '
G replaces only the first occurrence mode by default, and replaces all occurrences of the global pattern with the G option.
P If the substitution succeeds print the line, plus the P option invalidates the-n option
Note @: If multiple replacements are successful, multiple copies of this line will be printed
n A number between 1--512, replacing the first occurrence of the pattern (the default value is 1)
W file writes the row to the file if one substitution occurs
Note @: You can open up to 10 different file
Attached @:sed ': A; N $!ba, S/root/feyiz/4 ' #替换第4次出现的指定字符
Sed '/a/{x;s/^/./;/^.\{4\}$/{x;s/root/feyiz/;b};x} ' urfile #高效
Sed '/foo/s/foo/bar/g ' filename #高效替换
Sed '/foo/s//bar/g ' filename
Y conversion (Transform) characters--up to two address parameters
Note @: Convert characters by corresponding position one by one
Advanced commands to change the order of the process
N add next data to pattern space--up to two address parameters
Note @: In multi-line mode space, ^ matches only the first character in space, does not match the character following the line break; $ similar
Can be used to implement multi-line pattern matching
P Print the first row of data in a multiline pattern space created by the N command--up to two address parameters
Note @: After the last command in the script is executed, the contents of the schema space are automatically output by default
These three commands can be used to create an input/output loop that maintains a two-line pattern space, but only one row at a time, before the D command.
If N is not used for a row then the same as P
D Delete the first row of data in multiple lines of pattern space created by N, and re-edit with the first line of the script--up to two address parameters
Note @:d after deletion, the next data is not added to the pattern space and the remaining data is re-executed
Note @: If the command makes the pattern space empty, the next input line is read (as with the D command)
D is read into the next line after execution
The D command can change the command of the control flow in the script (the Delete command causes the new input line to be read, and the editing script starts a new round from the beginning)
G copy data from hold space to pattern space-up to two address parameters (overwrite the original data)
Note @: Add line breaks to pattern space if space is kept empty
G add data from hold space to pattern space--up to two address parameters (append)
Note @: Add line breaks to pattern space if space is kept empty
Copy multiple rows of data to a pattern space while maintaining multiple lines of space
H copy data from pattern space to hold space-up to two address parameters (overwrite the original data)
Note @: When there is more than one row of data in the pattern space, the multiple lines of the pattern space are copied to the hold space
H add data from pattern space to hold space--up to two address parameters (append)
Note @: Append the contents of the line break and pattern space to the hold space
Append line breaks Even if space is kept empty
X Exchange the contents of hold space and pattern space
: Label definition Labels
Note @: The label does not exceed 7 characters in length
No spaces are allowed between the colon and the label, and the space at the end of the line is considered part of the label
A space is allowed between a branch or a test command and a label, but no space can exist after the label
b label unconditionally jumps to label labels, if label is not specified, jumps to the end of the command (Loop, case, while)
Note @: Branch command for unconditional transfer
Can be easily understood as continue.
The label test is successfully replaced in the range of rows addressed, and if it is, jumps to the tag label; If no label is specified, jumps to the end of the command (until)
Note @: Test command for conditional transfer
Sed '/^aa/s/$/yes/;t;s/$/no/' Urfile #是AA就加YES, otherwise add NO
Sed '/^aa/ba;s/$/no/;b;:a;s/$/yes/' Urfile
Date +%m | Sed ' s/$/b12a01a02a03a04a05a06a07a08a09a10a11a12/;s/^\ (.. \) b.*\ (.. \) a\1.*/\2/' #利用lookup table takes the month of the month

SED command Learning

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.