Courseiva
Essential CommandshardMultiple ChoiceObjective-mapped

LFCS Essential Commands Practice Question

A developer needs to extract the third column from a CSV file where columns are separated by commas, but some fields contain commas within double quotes. Which command correctly handles this?

⚠ Common exam trap

It's easy for candidates to default to using `cut` or simple `awk` with a delimiter, not realizing that CSV files with quoted fields require a pattern-based field definition like `FPAT` to correctly parse embedded commas.

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 'BEGIN { FPAT = "[^,]*|\\"[^\\"]+\\"" } { print $3 }' file.csv

It uses `awk` with the `FPAT` variable to define a field pattern that correctly handles commas inside double-quoted fields. The pattern `[^,]*|\"[^\"]+\"` matches either a sequence of non-comma characters or a double-quoted string (including any commas within it), ensuring that the third column is extracted accurately even when fields contain embedded commas.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • cut -d',' -f3 file.csv

    Why it's wrong here

    cut does not handle quoted commas.

  • awk -F',' '{ print $3 }' file.csv

    Why it's wrong here

    Simple comma split fails on quoted commas.

  • awk 'BEGIN { FPAT = "[^,]*|\\"[^\\"]+\\"" } { print $3 }' file.csv

    Why this is correct

    FPAT regex matches non-comma sequences or quoted strings.

  • sed 's/[^,]*,[^,]*,\\([^,]*\\).*/\1/' file.csv

    Why it's wrong here

    sed regex fails on quoted commas.

About these practice questions

This LFCS question is part of Courseiva's 507-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This LFCS practice question is part of Courseiva's free Linux Foundation 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 LFCS exam.