sed notes and examples

The Unix command sed and some examples that I’ve used.

sed cheatsheat

:  # label
=  # line_number
a  # append_text_to_stdout_after_flush
b  # branch_unconditional
c  # range_change
d  # pattern_delete_top/cycle
D  # pattern_ltrunc(line+nl)_top/cycle
g  # pattern=hold
G  # pattern+=nl+hold
h  # hold=pattern
H  # hold+=nl+pattern
i  # insert_text_to_stdout_now
l  # pattern_list
n  # pattern_flush=nextline_continue
N  # pattern+=nl+nextline
p  # pattern_print
P  # pattern_first_line_print
q  # flush_quit
r  # append_file_to_stdout_after_flush
s  # substitute
t  # branch_on_substitute
w  # append_pattern_to_file_now
x  # swap_pattern_and_hold
y  # transform_chars

sed examples

# grabs an SSH fingerprint (aka, using ssh-keyscan or the AWS System Log). ONLY outputs the match (SSH public key)
sed -n 's/^.*\(ecdsa-sha2-nistp256 \)\(.*\)/\2/p'

# delete multiple lines using sed (from "^A" to "^B")
sed -i '/^A.*$/,/^B.*$/d' file.txt

# delete a trailing '.' in a line
sed 's/.$//g'

# replace the ASCII "*" with an actual "*"
sed 's/\\\\052/\*/g'