CompTIA Linux+ (XK0-004) — Question 309
An administrator has a CSV file named hosts.csv. The contents of hosts.csv include the following:
192.168.2.57,lnx1prd.example.com,Linux,Production
192.168.2.58,lnx2prd.example.com,Linux,Production
192.168.1.4,server15.example.com,Windows,Development
The administrator needs to create a second comma-separated list of only the Linux server IP addresses. Which of the following commands would achieve this need?
Answer options
- A. for ip in $(grep ג€Linuxג€ hosts.csv | cut ג€"dג€,ג€ ג€"f1); do echo ג€"n ג€$ip,ג€; done
- B. for ip in $( cut ג€"dג€,ג€ ג€"f1 hosts.csv | grep ג€Linuxג€); do echo ג€"n ג€$ip,ג€; done
- C. for ip in $(grep ג€Linuxג€ hosts.csv | sed ג€/$1//'); do echo ג€"n ג€$ip,ג€; done
- D. for ip in $(awk ג€"F, '{print $1}' hosts.csv | grep ג€Linuxג€); do echo ג€"n ג€$ip,ג€; done
Correct answer: A
Explanation
The correct command (A) uses grep to filter lines containing 'Linux' and then cuts out the first field, which corresponds to the IP addresses. Option B incorrectly attempts to cut before grepping, which does not guarantee that only Linux servers are processed. Option C uses sed incorrectly, and option D does not properly format the command for the intended output, making them ineffective for this task.