Sed Flashcards
What is sed?
An editor that allows us to use pattern matching to search and replace within a file, or an input stream.
How can you replace all the vowels of a file with asteriks in stdout?
sed “s/[aeoiu]/*/g” filename.txt
the quotes are for escaping
substitute/regexp/replacement/global flag
How can you replace only the second occurence of a pattern in a line using sed?
sed “s/regexp/replacement/2” filename.txt
By default, sed will print its output to _____.
STDOUT
What flag can you use to allow sed to edit the file in place, meaning your edits will be saved directly to the file?
-i
sed -i “s/regexp/replacement/flag” filename
How can you create a temporary file as a backup in case you make an error while using sed?
sed -i.tmp “s/regexp/replacement/flag” filename
How can you make a search pattern case insensitive with sed?
sed “s/regexp/replacement/i” filename
What is a sed script that will replace every character in a line that matches cha with the character *?
sed “/cha/s/./*/g” filename.txt
we preceed our search pattern with /cha/, telling sed to match the line against this pattern, it then substitutes the entire line (.) with *.
What variable can be used in the repalce section of your sed script to represent a match from the search part of the script?
&
How can we convert whatever character is saved in & to an uppercase version of that character?
with the \u character
sed ‘s/regexp/\u&/g’
How can you change characters to lowercase with sed?
using the \l character
sed ‘s/regexp/\l&/g’ filename
How can we suppress the default output of sed, printing nothing to the console window?
sed -n …
How can we supress all but the changes sed makes, printing only the affected lines to the console?
sed -n “s/regexp/replacement/p” filename
How can we write only modified lines to a separate file?
sed -n “s/regexp/replacement/pw new_filename” filename
How can we tell sed to delete any lines that match a specific pattern?
sed “/regexp/d” filename