Stream editor for filtering and transforming text
[me@linuxbox ~]$ echo "front" | sed 's/front/back/'
back
sed will accept any character that follows the character as the delimiter.
[me@linuxbox ~]$ echo "front" | sed 's_front_back_'
back~
Most commands in sed can be preceded by an address, specifying which lines of input streams are to be edited.
[me@linuxbox ~]$ echo "front" | sed '1s/front/back/'
back
Address | Description |
---|---|
n | A line number where n is a positive integer |
$ | The last line. |
regexp | Lines matching a POSIX basic regular expresion. |
Note that the regex is delimited by slash characters. | |
Optionally, the regex may be delimited by an alternate char, | |
by specifying the expression with , where c is alternate. | |
addr1,addr2 | A range of lines from addr1 to addr2, inclusive |
first~step | Match the line represented by the number first, then each |
subsequent line at step intervals. | |
addr1,+n | Match addr1 and the following n lines. |
addr! | Match all lines except addr, which may be any of the forms listed earlier |
sed Address Notation
Command | Description |
---|---|
= | Output the current line number |
a | Append text after the current line |
d | Delete the current line |
i | Insert text in front of the current line |
p | Print the current line. By default, sed prints every line and |
only edits lines that match specified address within the file | |
q | Exit sed without processing any more lines. If the -n option |
is not specified, output the current line. | |
Q | Exit sed without processing any more lines |
s/regex/replacement/ | Substitute the contents of replacement wherever regex |
y/set1/set2 | Transliterate by converting chars from set1 to chars in set2 |
sed Basic Editing Commands
Sources: #
https://www.thegeekstuff.com/2009/10/unix-sed-tutorial-advanced-sed-substitution-examples/ https://stackoverflow.com/questions/2232200/regular-expression-in-sed-for-masking-credit-card https://www.grymoire.com/Unix/Sed.html http://tldp.org/LDP/abs/html/x23170.html https://www.folkstalk.com/2012/01/sed-command-in-unix-examples.html