|
|
|
Lecture 6 — sed
|
|
|
|
|
regular expressions (regex)
|
|
|
|
|
exercise: phone number
|
|
|
|
|
matches the following
|
|
|
|
|
123-456-7890
|
|
|
|
|
(123) 456-7890
|
|
|
|
|
123 456 7890
|
|
|
|
|
123.456.7890
|
|
|
|
|
^\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$
|
|
|
|
|
grep -E examples
|
|
|
|
|
7-letter word, o is second letter, t is fifth
|
|
|
|
|
^.o..t..$
|
|
|
|
|
letters starting with q without a u
|
|
|
|
|
^q[^u]+
|
|
|
|
|
word with the same vowel
|
|
|
|
|
4 times: ([aeiou]).*\1.*\1.*\1
|
|
|
|
|
5 times: ([aeiou]).*\1.*\1.*\1.*\1
|
|
|
|
|
6 times: ([aeiou]).*\1.*\1.*\1.*\1.*\1
|
|
|
|
|
sed
|
|
|
|
|
grep can use regular expressions to find, sed can use them to find and replace
|
|
|
|
|
operates on one line at a line
|
|
|
|
|
sed —r e 's/PATTERN/REPLACEMENT/g' file
|
|
|
|
|
for each line in file, replaces every (longest) match for PATTERN with REPLACEMENT, and then print result to stdout
|
|
|
|
|
variations
|
|
|
|
|
leave out replacement to remove pattern
|
|
|
|
|
leave out g to replace only first match
|
|
|
|
|
add -n option and p to only output lines with a match
|
|
|
|
|
additional -e COMMAND to apply multiple find-and-replaces, left to right
|
|
|
|
|
echo $line | sed —r -e 's/ /,/g' -e 's/^(.*)$/\(\1\)/g'
|
|
|
|
|
sed -r —n -e 's/([0-9]+) ([0-9]+) ([0-9]+)/\(\1,\2,\3\)/gp’ < img_a.txt
|
|
|
|
|
exercise: write two sed commands (1) trim leading whitespace (2) trim trailing whitespace
|
|
|
|
|
sed -r -e ’s/^\s*//‘
|
|
|
|
|
sed -r -e ’s/\s*$//‘
|
|
|
|
|
can also filter with d
|
|
|
|
|
filter out blank lines: sed —r -e '/^\s*$/d'
|
|
|