Regular expressions with sed command (draft)

Source: Internet
Author: User

Regular expressions
. Match any single character
* Match any number of individual characters before it (can be a regular expression, such as. * represents any length that matches any character)
^ The regular expression followed by the match, at the beginning of the row or string
$ matches the preceding regular expression, at the end of a string or line
[...] Any character within the matching square brackets ((-) refers to the range of contiguous characters, (^) is the inverse meaning)
\{n,m\} matches the number of times that a single character in front of it recurs. \{n\} refers to reproducing n times;
\{n,\} is reproduced at least n times, \{n,m\} is reproduced n to M times
\{\} stores the pattern between \ and \ in a special "Reserved Space" (up to 9). Storage mode can be used from \1 to \9
For example: \{ab\}.*\1 represents two times of the AB combination and can have any number of characters in the middle.
{n,m} As with \{n,m\}, ERE (extended regular expression)
+ Match one or more instances of the preceding regular expression ERE
? Match 0 or one instance of the preceding regular expression ERE
| A regular expression that matches the before or after of the | symbol ERE Fast|slow (can match the sequence)
() matches the regular expression group in square brackets ERE

\ (ab\) \ (cd\) [def]*\2\1
\ (why\). *\1
\ ([[: alpha:]_][[:alnum:]_]*\) = \1
\ ([' ']\ '). *\1

SED command

sed [option] ' command ' input_file

The usual option
-n lists only those rows that have been processed by SED
-E perform SED action editing directly in instruction-list mode
-F filename writes the action of SED directly in a file
-R enables SED to support extended regular expressions
-I directly modifies the contents of the read file instead of the screen output

Select the rows you want to work with
/pattern/a line that matches the PATTERN of regular expressions
1 or 2 ... $ indicates the number of lines, $ represents the last line
1,$ represents the first line to the last row

The following commands are commonly used (the corresponding lines should be checked before using the command):
A \ Adds a string after the currently selected row
Sed ' 1a \add one ' test.txt adds a line after the first line add one
Sed ' 1, $a \add One ' test.txt adds a line after all rows from the first line to the last row add one
Sed '/first/a \ Add one ' test.txt adds a row after the line containing first:

C \ Replace the currently selected row with a string
Sed ' $c \add One ' Test.txt replaces the last line with the string add one
Sed ' 4, $c \add One ' test.txt replace the contents of line fourth to the last line with add one
Sed '/^ha.*day$/c \add one ' test.txt will start with HA and replace the line ending with day with add one

D Delete the currently selected row

i \ Inserts a string before the currently selected line

P Print the currently selected line to the screen

S/pattern/string/[g|i] Replace PATTERN strings with string
Sed '/line/text/g ' replaces all lines with Test G (global) without G will only replace our first line
Sed ' s/\ (. *\) line$/\1/g ' Delete all lines at the end of line

Regular expression with sed command (draft)

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.