Stream Editor

Updated: April 28, 2024

Sed is used to perform basic text transformations on an input stream. Essentially used for substitution or as a find and replace. Files can be edited without opening them!


Stream Editor

=       # output current line number
a       # append text after the current line
c		# change
d       # delete the current line
i       # insert text in front of the current line
p       # print the current line
q       # quit sed without processing any more lines, (if no -n, then output current line) 
Q       # quit sed without processing any more lines
;       # have more than one editing command  's/lazy/laxy/; s/jumped/jimped/'

-n      # no autoprint

'2,5p'  # range of lines to print

sed -n '/SUSE/!p' distros.txt   # all lines except those matched by regex

n       # line number
$       # last line
/regex/ # lines matching a regular expression

sed -f  applies to files.
sed -i    # In-Place File Editing  (make sure you list extension as could corrupt)

sed -i 's/lpve/love/; s/dsylight/daylight/' moon.txt  # changes first for the last 

echo "moon" | sed '1s/moon/mars/'  # changes moon to mars on line 1

echo "this is my Test" | sed 's/t/T/'   # Capaitalize all t's at beginning of a word
echo "this is my Test" | sed 's/t/T/g'  # g makes it global so all t's will capitalize

'3s/front/back/'  # substitute front with back on 3rd line