SED Worksheet
Our file sedtester contains:
The cat born
11/12/2012 guards the top of the stairs. The dog born 10/14/2013
is scared to cross that cat. That cat is funny.
--
Instructions
Just use sed to see the output without actually changing the file (so do not use the -i option). Do use -r to get the extended regex processing.
sed -r 'list of editing commands' filename
list of editing commands is similar to what you can use in VI in command mode.
Remember that the substitution command can use a regular expression
---
Write what these will produce when run over the file sedtester
Prework with Grep: Tell which line numbers will be chosen
G1 egrep '[0-9]{4}' sedtester. What number line is chosen?
G2 egrep '[born]' sedtester (Tell me why the last line is chosen.)
G3 egrep 's+' sedtester _____________________________________________
G4 egrep 'cat.*cat' sedtester _____________________________________________
G5 egrep '(.*).*\1' sedtester ____________________________________________
G6 egrep 'the?a?t?' sedtester _____________________________________________
G7 egrep 'the?a?t?' sedtester -o _____________________________________________
Simple sed using VI commands:
1.1. sed 2d sedtester __________________________________________________
1.2. sed = sedtester _________________________________________
1.3. sed iabc sedtester_________________________________________
1.4. sed 2iinsertedLine sedtester_________________________________________
1.5. sed 2cnewstuff sedtester ________________________________________
1.6 sed -n 1,2p sedtester ________________________________________
1.7 sed -n /cat/p sedtester ________________________________________
1.9 sed /cat/d sedtester ________________________________________
1.9 sed -r /cat/!d
sedtester ________________________________________
Sed transforming and simple substitution
2.1. sed y/abc/ABC/ sedtester ________________________________________
2.2. sed 2y/abc/ABC/ sedtester ________________________________________
2.4 sed s/cat/goat/ sedtester ________________________________________
2.4 sed s/cat/goat/g sedtester ________________________________________
2.3 sed 2,3s/cat/goat/g sedtester ________________________________________
SED regex
3.1 sed 's/^/Start of line /' sedtester ________________________________________
Note that the single quotes are required
3.2 sed 's/^is/IS/g' sedtester ________________________________________
3.3 sed -r 's/born$/created/' sedtester ________________________________________
Note -r is required
3.4 sed -r 's/[is]/[IS]/' sedtester _______________________________________________
3.5 sed -r 's/is|cat/WORD/' sedtester _______________________________________________
3.6 sed -r 's/(.)\1/DOUBLELETTER/g' sedtester ________________________________
3.7 sed -rn 's/[0-9]/FOUNDNUM/p' sedtester ___________________ __________________
3.8 sed -rn 's/.*([0-9]).*/\1/p' sedtester _______________________________________________
3.9 sed -rn 's/.*([0-9]{2}\/[0-9]{2}\/[0-9]{4}).*/\1/p' sedtester _____________________________
A little grep:
4.0 egrep -o '[0-9]{2}\/[0-9]{2}\/[0-9]{4}' sedtester _____________________________
4.1 egrep 'the|is|or' *
4.2 egrep 'the|is|or' * -l
4.3 egrep '(.)\1' sedtester