Sed is short for streameditor. It works perfectly with regular expressions. A common function of the sed command is text replacement. 1. sed can replace the string in the given text. It can be used with regular expressions to match... sed (stream editor) entry sed is the abbreviation of stream editor. It works perfectly with regular expressions. A common function of the sed command is text replacement. 1. sed can replace the string in the given text. It can be matched with a regular expression: $ sed's/pattern/replace_string/'file or $ cat file | sed's/pattern/replace_string/'// This command reads the input from stdin using the-I option. apply the replacement result to the source file. After replacement, redirection is usually used to save the file. $ Sed's/pattern/replace_string/'file> newfile replaces the source file with a new file: $ mv newfile file can also be completed with a line of command: $ sed-I's/pattern/replace_string/'file sed's/pattern/replace_string/'file can replace the content of the first matching style in each line, to replace all matching characters, you must add the parameter g at the end of the command, the following sed's/pattern/replace_string/g' file suffix/g indicates that sed will replace each match. When replacement is not required for each place, you can use/Ng to choose to replace from nth place, as shown below: sed's/pattern/replace_string/Ng 'file $ echo hello hellohello | sed's/hello/HELLO/2g' $ hello hello hellohello // output result character/In sed to the delimiter, we can also use any separator sed's: pattern: replace_string: g 'SED's | pattern | replace_string | g' when the delimiters appear in the style to be matched, use the prefix \ to escape them: sed's; he \; llo; replace_string; '2. the sed command contains multiple options for text processing. You can combine these options in a reasonable order to solve many complicated problems in a single line of command. As follows: a. remove blank rows: use '^ $' to match $ sed '/^ $/d' file B, and Mark matched strings (&) in sed, you can use the matched content when replacing the string with the & tag matching style string. $ Echo this is an example | sed's/\ w \=/ [&]/G' $ [this] [is] [an] [example] ps: \ w is a regular expression matching character (Perl-style regular expression), but not all tools in the following table support matching a word using the regular expression \ w \ +, then replace it. & Corresponds to the previously matched word. C. substring matching Mark \ 1 & represents a string that matches a given style. We can also match some of the given styles. $ Echo this is digit 7 in a number | sed's/digit \ ([0-9] \) /\ 1/'$ this is 7 in a number the preceding command replaces digit 7 with 7. The matched substring in the style is 7. $ echo seven EIGHT | sed's/\ ([a-z] \ + \) \ ([a-z] \ + \) /\ 2 \ 1/'$ EIGHT seven ([a-z] \ +) matches the first word, and ([a-z] \ +) matches the second word. \ 1 and \ 2 are used to reference them. This type of reference is called backward reference. In the replacement part, their order is changed to \ 2 \ 1, so the result is in reverse order. D. combine multiple expressions and combine multiple sed commands using pipelines. $ Sed 'expression' | sed 'expression1' is equivalent to: $ sed 'expression; expression1' e. The referenced sed expression is usually referenced in single quotes. However, double quotation marks can also be used. Double quotation marks are extended by evaluating the expression. For example, $ text = hello $ echo hello world | sed "s/$ text/HELLO/" $ HELLO world $ text is evaluated as hello.
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