A system administrator needs to analyze /var/log/secure for failed login attempts. For every line containing "Failed password", the administrator must extract and display only the timestamp (e.g., "Jul 31 10:45:01") and the source IP address from which the attempt originated. A sample log entry is:
Jul 31 10:45:01 server sshd: Failed password for root from 203.0.113.55 port 12345 ssh2
Which of the following commands is the most effective and reliable for this task?
The correct command is grep "Failed password" /var/log/secure | awk '{print $1, $2, $3, $11}'.
Here's the breakdown:
grep "Failed password" /var/log/secure first filters the log file to show only the lines containing the phrase "Failed password".
The output of grep is then piped to awk.
awk '{print $1, $2, $3, $11}' processes each line it receives. awk is a powerful tool for handling field-separated data. By default, it uses any sequence of whitespace as a delimiter, which makes it very robust for parsing log files where the number of spaces between fields can vary. It prints the first three fields ($1, $2, $3), which constitute the timestamp, and the eleventh field ($11), which is the source IP address.
Incorrect options explained:
grep "Failed password" /var/log/secure | cut -d' ' -f1-3,11 is incorrect because cut with a single space delimiter (-d' ') is not reliable for parsing log files. If there are multiple spaces between fields, cut will treat them as empty fields, leading to incorrect output.
awk '/Failed password/ {print $0}' /var/log/secure is incorrect because it does not perform the required extraction. The pattern /Failed password/ correctly filters the lines, but the action {print $0} prints the entire line ($0), not just the specific fields requested.
sed -n '/Failed password/p' /var/log/secure is incorrect because, like the previous awk command, it only selects and prints the entire matching lines. sed can be used to extract parts of a line with complex regular expressions, but it is not the most direct or effective tool for field-based extraction compared to awk.
Ask Bash
Bash is our AI bot, trained to help you pass your exam. AI Generated Content may display inaccurate information, always double-check anything important.
Why is `awk` more effective than `cut` for extracting specific fields from log files in this scenario?
Open an interactive chat with Bash
What is the role of `$1, $2, $3, $11` in the `awk` command?
Open an interactive chat with Bash
Can `sed` be used instead of `awk` for field-based extraction?
Open an interactive chat with Bash
CompTIA Linux+ XK0-006 (V8)
System Management
Your Score:
Report Issue
Bash, the Crucial Exams Chat Bot
AI Bot
Loading...
Loading...
Loading...
IT & Cybersecurity Package Join Premium for Full Access