Searching text with regular expressions
Regular expressions, and the two dialects Linux tools use: what basic and extended expressions differ on, the metacharacters worth memorising, and how grep and sed apply them.
Lesson 7 of 8 in objective 103. GNU and Unix commands, part of LPIC-1 Exam 101-500.
The metacharacters
. matches any single character. * means "zero or more of the thing before it" — so a* matches an empty string, which is the source of half the confusion about it. ^ anchors to the start of a line and $ to the end, so ^$ matches an empty line. Square brackets are a set: [aeiou] one vowel, [^0-9] one character that is not a digit, [a-z] a range. A backslash removes the special meaning of whatever follows, which is how you match a literal dot.
The extended metacharacters are +, ?, |, () and {}. + is one or more, ? is zero or one, | is alternation, parentheses group, and {2,4} is a repeat count. In BASIC regular expressions these need backslashes — \+, \?, \| — and in EXTENDED ones they do not. That is the entire practical difference between the dialects, and it is why the same pattern can work in egrep and fail in grep.
Which tool speaks which dialect
grep uses basic expressions; grep -E (the modern spelling of egrep) uses extended ones; grep -F (fgrep) turns the pattern off entirely and matches fixed strings, which is both faster and the right answer when the search text contains dots and asterisks you mean literally. sed uses basic expressions too, with sed -E for extended.
The grep flags that carry marks: -i ignores case, -v inverts the match, -c counts matching lines rather than printing them, -l prints only the names of matching files, -o prints only the matched part, -r searches recursively, and -n prefixes the line number. grep -c counts LINES, not matches, which is a distinction a question will lean on.
Worth carrying in
- ^ $
- Anchors: start of line, end of line. ^$ is an empty line.
- .
- Any single character.
- *
- Zero or more of the preceding element.
- [^0-9]
- One character not in the set.
- grep -E
- Extended expressions: + ? | ( ) { } work without backslashes.
- grep -F
- Fixed strings, no pattern interpretation.
- grep -v
- Invert: print lines that do NOT match.
- grep -c
- Count matching lines — lines, not matches.
- grep -o
- Print only the matched portion.
What the exam does with this
- BRE versus ERE: in basic expressions + ? | ( ) need escaping, in extended they do not.
- a* matches zero occurrences, so it also matches lines with no a at all. Expect a question built on that.
grep -ccounts lines containing a match, even if a line matches several times.
- Objective
- 103. GNU and Unix commands
- Share of the exam
- 43.33% (the whole objective)
- Questions in this lesson
- 15
- Signed for by a person
- 0
Partly checked. None of the 15 questions here has been read against the cited source by a person. 15 questions have been checked against their cited clause by an automated pass — which is not the same thing, and is not a signature.
Only questions a person has signed for are used in mock exams here. That is the whole difference between the two kinds of checking above.
How these questions are written — where each question comes from, what the verification ledger records, and what happens when one is found wrong.
Drill this lesson
A lesson is one sitting: the trainer draws a short run from these questions alone and spaces the ones you get wrong.
Practise Searching text with regular expressions
Questions in this lesson
- You must match one or more consecutive digits using grep with BASIC regular expressions (no -E). Which pattern is correct? machine-checked
- A colleague needs to search a file for the literal string `1.2.3.4` and is worried the dots will match any character. Which command treats the pattern as a fixed string with no regular expression meaning at all? machine-checked
- hosts.txt mixes host names with raw IP addresses. Which command uses a sed address to print ONLY the lines that begin with a digit, each line exactly once? machine-checked
- In `grep '\(ab\)\1' file`, what is the role of `\1`? machine-checked
- Using GNU grep, which THREE characters have their special meaning only when preceded by a backslash in a BASIC regular expression, but are special as typed in an EXTENDED one? machine-checked
- You need to know how many lines of /var/log/auth.log contain the text `Failed password`. Which command reports exactly that number? machine-checked
- Using grep with basic regular expressions and POSIX character class syntax, which pattern matches any line containing at least one decimal digit? machine-checked
- A colleague is surprised that `grep 'ab*c' data.txt` matches a line containing the string `ac`. What explains the match? machine-checked
- You run `sed 's/http:/https:/' urls.txt` and find that on lines holding two URLs only the first was rewritten. Which command fixes it? machine-checked
- You must change 8080 to 8443 inside /etc/app/app.conf, saving the result in the file itself while keeping the original as app.conf.bak. Which command does that with GNU sed? machine-checked
- A configuration file is to be displayed with every empty line and every line that begins with # in the first column removed. Select the TWO commands that do this. machine-checked
- Type the complete command that searches every file below /etc recursively for the string nameserver and prints only the names of the files that contain it, not the matching lines. machine-checked
- Type the complete sed command that prints only the fifth line of data.txt and nothing else, using the option that suppresses sed's automatic printing together with an explicit print command. machine-checked
- You must find lines in /var/log/auth.log that mention root as a complete word, so that chroot and rootkit are not reported. Select the TWO commands that achieve this with GNU grep. machine-checked
- Every run of digits in report.txt is to be wrapped in square brackets, so that `code 42 rev 7` becomes `code [42] rev [7]`. Which command does that? machine-checked
Practise Searching text with regular expressions
The rest of objective 103
- Working on the command line
- Filtering text streams
- Basic file management
- Streams, pipes and redirection
- Creating, monitoring and killing processes
- Process execution priorities
- Searching text with regular expressions — you are here
- Editing files from the terminal