Extract only the first field from services.csv.
Remove duplicates and print the names in alphabetical order: api, db, web.
api,west,7
web,east,3
api,west,7
db,east,4
web,east,3
api,east,2Which command is appropriate?
uniq removes adjacent duplicate lines, so sort must place identical service names next to each other first. sort -u is a shorter alternative.
Detailed explanation
cut -d, -f1 services.csv | sort | uniqCorrect. It extracts field 1, sorts the names, and then removes adjacent duplicates.
For the supplied simple comma-separated rows, extraction produces api, web, api, db, web, api. Ordering groups the equal names together before adjacent repeats are collapsed. The resulting names are api, db, and web.
sort services.csv | cut -d, -f1Incorrect. It sorts whole CSV lines and extracts field 1, but never removes duplicates.
Ordering the complete CSV rows does not remove duplicate service names. After the first column is extracted, api still appears three times and web twice. A changed order is not evidence of uniqueness.
cut -d, -f1 services.csv | uniq | sortIncorrect. uniq only removes adjacent duplicates; repeated names that are separated in the input remain.
In the supplied first-column stream, repeated api and web entries are separated by other names. The adjacent-repeat stage lets them through; ordering the result later does not revisit that earlier stage to remove them.
cut -d, -f2 services.csv | sort -uIncorrect. It extracts field 2, producing east and west rather than service names.
The second column contains regions, so the unique output is east and west. Combining sorting with uniqueness is useful here, but it cannot correct extraction of the wrong column.
Try it yourself
An example you can run in a temporary verification environment.
cut -d, -f1 services.csv | sort | uniq
# Shorter alternative
cut -d, -f1 services.csv | sort -uOfficial references
Expected result
api
db
webKey points
- cut delimiters and field selection
- Why sort is needed before uniq
- The sort -u shorthand
Notes
- Environment: Bash with GNU Coreutils. Save the supplied six lines in a temporary services.csv file. Collation order can depend on the locale.
- Compare both supplied verification pipelines with the expected three lines. For these options and this data, sort -u combines ordering and duplicate removal.
- Use a temporary copy of the six supplied rows. LC_ALL=C provides a predictable byte-oriented comparison for this ASCII example; more complex text can sort differently under other locales.
Foundation review
Keep each intermediate stream visible
The first extracted column has six names. Sorting changes their order; uniqueness changes how many repeated lines remain. Follow both the values and their counts rather than judging a command name alone.
Input order is a precondition
GNU uniq does not require sorted input, but it detects repeated lines only when they are adjacent. Sorting first is a way to make every equal name adjacent for general duplicate removal.
Understand the simple CSV assumption
The supplied rows have no quoted commas or embedded newlines. cut -d, is appropriate for those rows; it is not a general parser for every CSV quoting rule. Use a CSV-aware parser when those features are present.