sed
Erik Fredericks, frederer@gvsu.edu Fall 2025
Based on material provided by Erin Carrier, Austin Ferguson, and Katherine Bowers
Manipulates input (stream or file)
Typically:
sed [options] '/pattern/ command' input_file
sed [options] 'range command' input_file
-E to use extended regex
-E
-f filename to load sed commands from a file
-f filename
--in-place or -i to modify a file in place
--in-place
-i
--in-place=suffix
-isuffix
sed '4 d'
sed '1,4 d'
sed -E '/pattern/ d'
($ is the end of file when using ranges!)
$
sed -E 's/pattern/replace_str'
sed -E 's/ *$//' filename
By default, only replaces first match per line
sed -E 's/pattern/replace_str/g'
sed -E 's/pattern/replace_str/2'
Can combine patterns with ranges!
sed -E '5,$ s/pattern/replace_str/'
Can also use parts of matched pattern in replacement!
Uses (parentheses groupings)
To refer to parentheses groups: \1, \2, ...
\1
\2
sed -E 's/the ([[:alpha:]]+) /\1/
To refer to the whole matched pattern: &
&
sed -E 's/(.+\.com)/www.&/'
https://www.geeksforgeeks.org/linux-unix/sed-command-in-linux-unix-with-examples/