The line id=12,id=34 is in ids.txt.
Print only the number strings 12 and 34, each on its own line, using GNU grep.
Which command is appropriate?
GNU grep -o prints only the matching portions. Multiple matches on one input line are emitted as separate output lines.
Detailed explanation
grep -E '[[:digit:]]+' ids.txtIncorrect. It prints the whole input line rather than each matched number separately.
Incorrect. It prints the whole input line rather than each matched number separately.
grep -F '[[:digit:]]+' ids.txtIncorrect. -F treats the bracket expression and plus sign literally.
Incorrect. -F treats the bracket expression and plus sign literally.
grep -Eo '[[:digit:]]' ids.txtIncorrect. It prints one digit at a time rather than grouping each number.
Incorrect. It prints one digit at a time rather than grouping each number.
grep -Eo '[[:digit:]]+' ids.txtCorrect. -o isolates each run of one or more digits.
Correct. -o isolates each run of one or more digits.
Try it yourself
An example you can run in a temporary verification environment.
printf '%s\n' 'id=12,id=34' | grep -Eo '[[:digit:]]+'Expected result
12
34Key points
- -o prints only matched text
- Multiple matches are separated
- + groups consecutive digits
Notes
- Environment: GNU grep 3.x / LC_ALL=C
- 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.