sed
find and replace, without an editorsed — stream editor — applies an editing command to every line of its input. Its substitute command, s/old/new/, is find-and-replace from the command line, and with -i it changes the file itself.
s/old/new/g — substitute, the pattern, the replacement, and g for every occurrence on the line.
At the terminal
Read the session first, then the notes: each numbered line in the output is answered by the note with the same number.
What the numbered lines are telling you
The result is printed. links.txt on disk still says http: — nothing was written.
-n stops sed printing everything, and 2p prints line 2 only. It shows the file is unchanged.
The file is rewritten, and the original is saved as sshd_config.bak. ^ anchors the pattern to the start of the line.
Two commands separated by a semicolon: delete lines starting with #, and delete empty lines. What is left is the active configuration.
Switches
A switch changes how the command behaves. These are the ones used day to day; man sed lists the rest. Single-letter switches can usually be joined, so -l -a may be written -la.
-i- Edit the file in place. -i.bak also keeps a backup.
-n- Do not print each line automatically; print only what p asks for.
-e SCRIPT- Add another editing command.
-E- Use extended regular expressions.
-f FILE- Read the editing commands from a file.
Variations worth knowing
The same tool, pointed at a narrower question. These four cover most of what it is reached for day to day.
sed -n '10,20p' file.txtPrints lines 10 to 20 only.
sed 's/ *$//' file.txtRemoves trailing spaces from every line.
sed -i 's/\r$//' script.shRemoves Windows line endings from a script.
sed 's|/var/www|/srv/www|g' configUses | as the delimiter, so paths need no escaping.
In cybersecurity
sed -i rewrites a file with no confirmation and no undo.
-i.bak costs nothing and saves the original. Run the command without -i first to see the result.
A dot matches any character and unanchored text matches anywhere in a line. Anchor with ^ and $, and escape the dots.
sed is how hardening scripts flip settings across many files. Check the result with grep afterwards — a pattern that matched nothing fails silently.