An administrator needs to extract the user name, home directory, and login shell from /etc/passwd and print them on one line separated by commas. Their first attempt is:
$ IFS=: awk '{print $1,$6,$7}' /etc/passwd
The output still contains spaces instead of commas. Which replacement command will generate the desired comma-delimited output without changing the shell's default word-splitting behavior?
The awk utility, not the shell, performs the field parsing and record reconstruction. Option -F: tells awk to use a colon when splitting each input record into fields, and -v OFS=',' sets the output field separator to a comma. Thus
awk -F: -v OFS=',' '{print $1,$6,$7}' /etc/passwd
reads the colon-delimited data correctly and prints user,home,shell with commas, while leaving the shell's IFS unchanged.
Setting the shell variable IFS before calling awk (second choice) has no effect on awk, so the output remains space-separated. The third choice changes FS after the record has already been split, so awk still uses the default whitespace separator and prints the wrong fields. The last command uses a comma as the input separator; because /etc/passwd is colon-delimited, no splitting occurs and each whole line is printed unchanged.
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.
What does the -F option in awk do?
Open an interactive chat with Bash
What is the purpose of OFS in awk?
Open an interactive chat with Bash
Why does setting IFS in the shell not affect awk?
Open an interactive chat with Bash
CompTIA Linux+ XK0-006 (V8)
Automation, Orchestration, and Scripting
Your Score:
Report Issue
Bash, the Crucial Exams Chat Bot
AI Bot
Loading...
Loading...
Loading...
IT & Cybersecurity Package Join Premium for Full Access