Shell programming (7) --- usage of SED

Source: Internet
Author: User

Sed: A stream editor or a row editor.

Sed processing process: sed reads a row from the source file and places it in a temporary buffer zone. This buffer zone is called a mode space and uses the corresponding mode to process the mode space, the processing result is displayed on the screen without affecting the source file. After processing, the content in the mode space is displayed on the screen.


Sed format:

 
Sed [Options] 'addresscommand' file... options:-N: silent mode. Content in the mode space is not displayed. Content in the default space is displayed by default. It is often used together with the p command. -I: directly modify the content of the source file. -E 'sssscommand'-e 'sssscommand': multiple scripts are executed simultaneously. -F: Specifies the path of the SED script file. -R: by default, sed only supports basic regular expressions. To use an extension expression, you must use the-r option.

Address format

 
1. Startline, endline such as: 1,100: indicates from the first row to the 100th row. $: Indicates the last row. 2./Regexp/regular expression. For example,/^ root/indicates the row starting with root. 3./pattern1/,/pattern2/indicates all rows in the middle starting from the row first matched by pattern1 mode to the end of the row with patter2 match for the first time. 4. linenumber indicates a specified row number. 5. Startline, + N indicates the N rows starting from the Startline line and backward, n + 1 rows in total, including the current row and the N rows following it.


Command Format

 
D: delete rows that meet the conditions. P: displays rows that meet the conditions. A \ "string": append a new row to the specified row with the content of string. To append multiple rows, use \ n.

Example:

 
[[Email protected] TMP] # Cat bond0 | sed '/userctl/A \ hello1 \ nhello2' device = bond0bootproto = noneonboot = yestype = ethernetipaddr = container 255.255.0gateway = repository = Repository yes [[email protected] TMP] #
I \ string: Add a new line before the specified row with the content of string. Add multiple rows and use \ n. R file: reads the content of the file after the specified row and adds it to the row. W file: saves content within the specified range to the file. S/pattern/string/: Replace the content matched by pattern with string. Only Replace the first pattern-matched string in each line. S/pattern/string/G: Replace the content matched by pattern with string. Replace all matched modes in each row. S/pattern/string/I: replace the Matching content of pattern with string. Case sensitivity is ignored during search. Note: The/after s can be a separator, and many other characters can also be used as separators, such as the format of S # root # frame # G.


Metacharacters in Pattern

^: The First Line identifier. For example,/^ My/indicates that all rows starting with my are matched. $: Locate the end of a row. For example,/my $/indicates matching all rows ending with my ..: Match any single character except line breaks. For example,/M.. Y/matches the string containing the letter M, followed by two arbitrary characters, and followed by the row with the letter y. *: Indicates that the first character is matched 0 or multiple times. For example,/my */matches a row that contains letters M followed by zero or multiple y letters. []: Any character in the matching range. For example,/[mm] Y/matches the rows that contain my or my. [^]: Match any character outside the specified range. For example,/[^ mm] Y/matches y, but the character before y is not a line of M or M. \ (.. \): Save matched characters. For example, 1, 20 s/\ (you \) Self/\ 1R/mark the mode between metacharacters and save it as tag 1. Then you can reference it with \ 1. A maximum of nine tags can be defined, starting from the left and the first tag on the left. In this example, 1st to 20th rows are processed, and you are saved as tag 1. If you find youself, replace it with your. &: Save the search string to reference it in the replacement string. For example, S/My/** & **/Symbol & indicates the search string. My will be replaced with ** my ** \ <: The first Separator of the word. For example,/\ <my/matches rows that contain words starting with my. \>: Suffix. For example,/My \>/matches the rows that contain words ending with my. X \ {M \}: m x in a row. For example,/9 \ {5 \}/matches rows that contain 5 consecutive 9 records. X \ {M, \}: At least m x. For example,/9 \ {5, \}/matches rows containing at least five consecutive 9 x \ {M, N \}. At least m, but no more than N x. For example,/9 \ {5, 7 \}/matches rows containing 5 to 7 9 consecutive times. The following are also available: [: alnum:] [: Alpha:] [: blank:] [: cntrl:] [: digit:] [: Graph:] [: lower:] [: Print:] [: punct:] [: Space:] [: Upper:] [: xdigit:]





This article is from the "Hezhang" blog, please be sure to keep this source http://hezhang.blog.51cto.com/1347601/1435548

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.