103.7 Search text files using regular expressions Flashcards
What command do you usually use with regular expressions?
grep
What does a dot (.) represent in a regex?
A dot represents a single character. More than one dot can be used consecutively to represent more unknown characters in a row.
What symbol do you use to search for a string at the beginning of a line and at the end of a line in a regex?
At the beginning: ^
At the end: $
What syntax would you use to search for a line that contains a specified character using a regex?
grep [x]
What syntax would you use to search for a line that contains a string at the end using a regex?
grep bash$
What syntax would you use to search for a line that contains a string the beginning using a regex?
grep ^rpc
What syntax would you use to search for a line that contains either a capital or a lowercase character using a regex?
#grep -i [x] #grep [Xx]
What syntax would you use to search for a line in a file that begins with either a capital or lowercase A, then followed by any character and the third character being either a capital or lowercase A?
#grep ^[Aa] .[Aa] $grep -i ^[a].[a]
What symbols do you use to exclude a character from a search in a regex?
[^abc]
What does the star sign mean in a regex?
It matches zero or more of the preceding characters or expression.
What does the #egrep command do?
It searches a specified file line by line returning lines that contain a pattern matching a given regular expression.
What is the equivalent of egrep?
grep -E
How do you egrep a file for lines that contain {bash} at the end of the lines?
egrep ‘bash$’
How do you egrep a file just to know how many lines contain the string at the end of the lines?
egrep -c ‘bash$’
How do you egrep a file to find lines that contain a string at the beginning or another string at the end of the lines?
egrep ‘^rpc|nologin$’
What does the fgrep command do?
It searches based on strings rather than patterns. Also uses file globbing instead of regexes.
It equals to grep -F
What is the syntax for the #sed command to replace a string for another globally in a whole document?
sed ‘s/orignalstring/newstring/g’
In the #sed command, what flag do you use to make the stream search case insensitive? What flag do you use to prevent another file being created?
#sed -I (case insensitive) #sed -i (in place)
What is the syntax for the #sed command to make a change permanent in a file?
sed -i ‘s/orignalstring/newstring/g’
What is the syntax for the #sed command?
sed ‘s/orignalstring/newstring/’
What is the command that is usually used to search and replace words in a file?
sed