h1. Sed syntax {{>toc}} This syntax format is described in standard "Unix sed utility in IEEE Std 1003.1-2001, Portable Operating SystemInterface (POSIX ), Shells and Utilities". h2. Special match symbols |
^|begin of string| |
$|end of string| |
.|any symbol| |
*|previous value is repeated 0 or more times| |
+|previous value is repeated 1 or more times| |
{N}|previous value is repeated exactly N times| |
{N,M}|previous value is repeated from N to M times| |
[[:digit:]]|any digit| To use specials symbols you need to screen them with '\'. For example, '\+'. h2. Special replacement symbols |
&|whole matched string| |
\1|1 matched atom| h2. Examples |*search*|*replace*|*result*| |
^011.*|
777|string 011XXXXX will be replaced with string 777| |
^011|
7|string 011XXXXXXX will be replaced with 7XXXXXXX| |
.*|
00&|to all strings will be added a prefix 00. & - means "put original string"| |
^011||a prefix 011 will be cut| |
^(8)(.{10})$|
7\2|strings 8ХХХХХХХХХХ will be replaced with 7ХХХХХХХХХ. All strings, which contain less or more 11 digits won't match this rule. Original string is split into 2 "atoms" - digit 8 and 10 remaining digits. \2 - means, that we put 2nd "atom" - 10 digits after 1st digit 8| |
(^.*)(.{2}$)|
749511111\2|system remembers 2 last digits of original string and they are added to string in field "replace". Therefore, if original string was 444455, the resulting string will be 74951111155| |
.*|
&|any value will pass without changes. & - means "put original string"| |
^[[:digit:]]*$|
&|digital string will pass without changes. & - means "put original string"| |
^\+||prefix '+' will be cut. '\' - is a symbol of screening of a special symbol '+'| [[Синтаксис sed|Русский перевод]]