LPIC-1 Shells, Scripting and Data Management Practice Question
An administrator needs to extract only the fifth field from a colon-delimited file, but some lines contain fewer than five fields and must be skipped rather than printed as empty. Which awk program accomplishes this?
⚠ Common exam trap
The trap here is testing whether $5 is empty instead of testing NF, which cannot distinguish a missing field from a field that exists but contains no characters.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
awk -F: 'NF >= 5 {print $5}' file
The requirement is to skip records that lack a fifth field, which is exactly what the NF field-count variable expresses. Testing NF >= 5 before printing $5 rejects short lines at the source, whereas tests on the value or length of $5 conflate an absent field with a present-but-empty one and can silently drop legitimate empty fields. Using NF keeps the decision tied to the record's structure.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
awk -F: '$5 != "" {print $5}' file
Why it's wrong here
Testing $5 != "" skips fields that are present but empty, and it also skips a line whose fifth field is genuinely blank. More importantly, on a line with only four fields awk still assigns $5 the empty string, so this test happens to skip it, but it also drops legitimate empty fifth fields, which is not the same as skipping short lines.
- ✗
awk -F: '{print $5}' file | grep -v '^$'
Why it's wrong here
This prints the fifth field for every line, including short lines where it is empty, then filters out empty results. The output may look correct, but it also discards lines whose fifth field is legitimately empty, and it loses the distinction between a missing field and an empty one. It relies on post-filtering rather than the field count, which is less precise.
- ✗
awk -F: 'length($5) > 0 {print $5}' file
Why it's wrong here
Testing the length of the fifth field is equivalent to testing that it is non-empty, so it skips lines with a present but empty fifth field while accepting any non-empty value. It does not check NF, so a line with four fields and a non-empty fourth field still prints an empty $5 only if length is zero, meaning the logic targets emptiness rather than missing fields.
- ✓
awk -F: 'NF >= 5 {print $5}' file
Why this is correct
NF holds the number of fields on the current record, so NF >= 5 selects only lines that actually contain at least five colon-separated fields, and print $5 emits the fifth one. Lines with fewer fields are never printed, exactly matching the requirement to skip short lines rather than emit empty output. This is the idiomatic awk approach.
Go deeper
Related to this question
About these practice questions
One of 402 original LPIC-1 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
JA
Written and reviewed by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
Last reviewed September 2026 · checked against the official LPI exam blueprint
This LPIC-1 practice question is part of Courseiva's free LPI certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the LPIC-1 exam.