V Lecture 6 — sed
V regular expressions (regex)
V exercise: phone number
V matches the following
* 123-456-7890
* (123) 456-7890
* 123 456 7890
* 123.456.7890
* ^\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$
V grep -E examples
V 7-letter word, o is second letter, t is fifth
* ^.o..t..$
V letters starting with q without a u
* ^q[^u]+
V 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
V sed
* grep can use regular expressions to find, sed can use them to find and replace
* operates on one line at a line
V 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
V 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
V 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
V exercise: write two sed commands (1) trim leading whitespace (2) trim trailing whitespace
* sed -r -e ’s/^\s*//‘
* sed -r -e ’s/\s*$//‘
V can also filter with d
* filter out blank lines: sed —r -e '/^\s*$/d'