Select lines containing status=500 from access.log.
Count the selected lines without creating a temporary file.
Which command list is correct?
A pipe sends the left command's standard output to the right command's standard input. grep filters, and wc -l counts the resulting lines.
Detailed explanation
grep 'status=500' access.log > wc -lIncorrect. > redirects output to a file named wc rather than invoking wc -l.
Incorrect. > redirects output to a file named wc rather than invoking wc -l.
grep 'status=500' access.log < wc -lIncorrect. < supplies input to grep and does not connect its output to wc.
Incorrect. < supplies input to grep and does not connect its output to wc.
grep 'status=500' access.log | wc -lCorrect. grep's matching lines flow directly into wc -l.
Correct. grep's matching lines flow directly into wc -l.
wc -l access.log | grep 'status=500'Incorrect. It filters the numeric output of wc rather than counting grep matches.
Incorrect. It filters the numeric output of wc rather than counting grep matches.
Try it yourself
An example you can run in a temporary verification environment.
printf 'status=200
status=500
status=500
' | grep 'status=500' | wc -lExpected result
2(前の空白幅は環境により異なる)Key points
- | connects output to input
- grep selects lines
- wc -l counts result lines
Notes
- Environment: Bash 5.2 / GNU grep・coreutils
- 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.