Skip to main content

Regular expressions

Character Description ^ Matches the beginning of the line $ Matches the end of the line . Matches any single character * Will match zero or more occurrences of the previous character [ ] Matches all the characters inside the [ ]
Regular expression Description /./ Will match any line that contains at least one character /../ Will match any line that contains at least two characters /^#/ Will match any line that begins with a '#' /^$/ Will match all blank lines /}$/ Will match any lines that ends with '}' (no spaces) /} *$/ Will match any line ending with '}' followed by zero or more spaces /[abc]/ Will match any line that contains a lowercase 'a', 'b', or 'c' /^[abc]/ Will match any line that begins with an 'a', 'b', or 'c'

SED Examples:
sed -i ’s/Ben/Dave/g’ file.txt - Replace all the words Ben for the word Dave
sed 's/Ben|ben/Dave/g' file.txt - Replace all the words Ben and ben for the word Dave
sed 's/^[ ^t]*//' file.txt - Delete all spaces in front of every line of file.txt
sed 's/[ ^t]*$//' file.txt - Delete all spaces at the end of every line of file.txt
sed -e '/^#/d' file.txt | more - View file without the commented lines
sed -e '/regexp/d' file.txt - delete the word regexp
sed 's/...//' - delete the first 3 characters on every line

CUT:
cut -d " " -f 1 - cut everything after the first word