sed which stands for stream editor is a unix utility that was developed by Lee E. McMahon at Bell Labs in the 70s. It is a powerful text processing utility.
sed 's/original-string/replacement-string/[flags]' [input-file]
# to remove everything from a line before a word
sed 's/.*WORD//'
# remove everything from a line after a character or pattern
sed 's/WORD.*//'
# extract everything between two words
sed 's/.*START//; s/END.*//'
# clean up white space in the front of the output globally
sed 's/^[[:space:]]*//g'
# clean up white space in the end of the output globally
sed 's/[[:space:]]*$//g'
# clean up white space in the the front and in the end of the output globally
sed 's/^[[:space:]]*//g; s/[[:space:]]*$//'
# extract everything between two words and clean up white space
sed 's/.*START//; s/END.*//; s/^[[:space:]]*//g; s/[[:space:]]*$//'
# replace all null in the file with |
sed 's/null/|/g' change.csv > output.csv
sed 's/old_text/new_text/' filename
sed '5s/old_text/new_text/' filename
sed '/pattern/s/old_text/new_text/' filename
sed '/pattern/ s/old_text/new_text/' filename
sed '/pattern/d' filename
sed '/^$/d' filename
sed 's/^/prefix/' filename
sed 's/$/suffix/' filename
sed -n '5p;10p' filename
sed 's|old_text|new_text|' filename
sed '3,7s/old_text/new_text/' filename
sed 's/^[ \t]*//' filename
sed 's/old_text/new_text/' filename
sed '/pattern/i\inserted_line' filename
sed '/pattern/a\appended_line' filename
sed 's/<[^>]*>//g' filename.html
sed 's/\b\(.\)/\u\1/g' filename
sed '10,20r file_to_insert.txt' filename
sed '1!G;h;$!d' filename
sed 's/[ \t]*$//' filename
Option | Description | Example |
---|---|---|
-n |
Suppress default pattern space printing | sed -n '3 p' test.txt |
-i |
Backup and modify input file directly | sed -ibak 's/John/Johnny/' test.csv |
-f |
Execute sed script file | sed -f sedscript.sed test.txt |
-e |
Execute multiple sed commands | sed -e 'command1' -e 'command2' -e 'command3' file.txt |
Flag | Description | Example |
---|---|---|
g |
Global substitution | sed 's/search/replace/g' file.txt |
1,2... |
Substitute the nth occurrence | sed 's/search/replace/2' file.txt |
p |
Print only the substituted line | sed -n 's/search/replace/p' file.txt |
w |
Write only the substituted line to a file | sed -n 's/search/replace/w out.txt' file.txt |
i |
Ignore case while searching | sed 's/search/replace/i' file.txt |
e |
Substitute and execute in the command line | sed -e 's/^/ls -l /' file.txt |
~/|^@! |
Substitution delimiter can be any character | sed 's@/usr/local/bin@/usr/bin@' file.txt |
& |
Gets the matched pattern to use for replacement | sed 's/^.*/<&>/' file.txt #Encloses whole line between < and > |
\( \) |
Group using \( and \). Use \1, \2 for replacement | sed 's/\([^,]*\),\([^,]*\),\([^,]*\).*/\1,\2/g' file.txt |
Command | Description | Example |
---|---|---|
p |
Print Pattern space | sed -n '1,5 p' file.txt |
d |
Delete lines | sed -n '1,5 d' file.txt |
w |
Write pattern space to file | sed -n '1,5 w output.txt' file.txt |
a |
Append line after | sed '4 a replacement-line' file.txt |
i |
Insert line before | sed '5 i replacement-line' file.txt |
c |
Change line | sed '10 c replacement-line' file.txt |
l |
Print hidden characters | sed -n l file.txt |
= |
Print line numbers | sed = file.txt | sed '{N;s/\n/ /}' |
y |
Change case | sed 'y/abcde/ABCDE/' file.txt |
q |
Quit sed | sed '6 q' file.txt |
r |
Read from file | sed '$ r sample.txt' file.txt |
# |
Comment inside sed script | # |
Command | Description |
---|---|
n |
Print pattern space, empty pattern space, and read next line. |
x |
Swap pattern space with hold space |
h |
Copy pattern space to hold space |
H |
Append pattern space to hold space |
g |
Copy hold space to pattern space |
G |
Append hold space to pattern space |
Command | Description |
---|---|
b label |
Branch to a label (for looping) |
t label |
Branch to a label only on successful substitution (for looping) |
:label |
Label for the b and t commands (for looping) |
N |
Append next line to pattern space |
P |
Print 1st line in multi-line |
D |
Delete 1st line in multi-line |
References: (n.d.). Retrieved from https://www.gnu.org/software/sed/manual/sed.txt