Sed Flashcards

1
Q

What is sed?

A

An editor that allows us to use pattern matching to search and replace within a file, or an input stream.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can you replace all the vowels of a file with asteriks in stdout?

A

sed “s/[aeoiu]/*/g” filename.txt

the quotes are for escaping
substitute/regexp/replacement/global flag

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How can you replace only the second occurence of a pattern in a line using sed?

A

sed “s/regexp/replacement/2” filename.txt

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

By default, sed will print its output to _____.

A

STDOUT

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What flag can you use to allow sed to edit the file in place, meaning your edits will be saved directly to the file?

A

-i

sed -i “s/regexp/replacement/flag” filename

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can you create a temporary file as a backup in case you make an error while using sed?

A

sed -i.tmp “s/regexp/replacement/flag” filename

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can you make a search pattern case insensitive with sed?

A

sed “s/regexp/replacement/i” filename

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a sed script that will replace every character in a line that matches cha with the character *?

A

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 *.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What variable can be used in the repalce section of your sed script to represent a match from the search part of the script?

A

&

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How can we convert whatever character is saved in & to an uppercase version of that character?

A

with the \u character

sed ‘s/regexp/\u&/g’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can you change characters to lowercase with sed?

A

using the \l character

sed ‘s/regexp/\l&/g’ filename

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How can we suppress the default output of sed, printing nothing to the console window?

A

sed -n …

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can we supress all but the changes sed makes, printing only the affected lines to the console?

A

sed -n “s/regexp/replacement/p” filename

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How can we write only modified lines to a separate file?

A

sed -n “s/regexp/replacement/pw new_filename” filename

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How can we tell sed to delete any lines that match a specific pattern?

A

sed “/regexp/d” filename

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What operator can we use to run multiple sed commands at the same time?

A

;

17
Q

How can we use sed to strip a file of HTML tags?

A

sed ‘s/]*>//g;/^$/d’ filename

this uses the ; operator to combine two kinds of searches

18
Q

Sed is a very simple editor that allows us to quickly __________.

A

files or streams

19
Q

A basic understanding of sed can come in very handy when you’re working on a _____.

A

bare machine that has yet to have a more sophisticated editor installed

20
Q

Sed can be easily ____, making it a great tool to use in deployment scripts to __________ or other similar operations.

A

scripted

update configuration files