devices.txt contains disk, disk0, disk7, disk12, and diskA.
Extract only lines whose entire value is disk followed by one digit.
Which basic regular expression is appropriate?
[[:digit:]] matches one digit. Anchoring it between ^disk and $ excludes missing, extra, or nonnumeric suffixes.
Detailed explanation
^disk.$Incorrect. . matches any single character, including A.
Incorrect. . matches any single character, including A.
^disk[:digit:]$Incorrect. Without brackets, [:digit:] is not a character class.
Incorrect. Without brackets, [:digit:] is not a character class.
^disk[[:digit:]]$Correct. The POSIX digit class matches exactly one digit between the anchors.
Correct. The POSIX digit class matches exactly one digit between the anchors.
^disk[0-9]*$Incorrect. * permits zero or multiple digits, including disk and disk12.
Incorrect. * permits zero or multiple digits, including disk and disk12.
Try it yourself
An example you can run in a temporary verification environment.
printf 'disk\ndisk0\ndisk7\ndisk12\ndiskA\n' | grep '^disk[[:digit:]]$'Expected result
disk0
disk7Key points
- POSIX character classes
- The class matches one character
- Anchors constrain the whole line
Notes
- Environment: GNU grep 3.x / C.UTF-8ロケール
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.