Each Linux text utility does one small job, but pipelines turn them into powerful workflows. They read ordinary text and write to standard output, which makes them ideal for logs, lists, and delimiter-separated data.
Count with wc
wc file.txt prints line, word, and byte counts. Use wc -l, wc -w, or wc -c when you need only one result. For example, wc -l access.log quickly counts log records.
Order with sort
sort names.txt orders lines lexically. -n compares numeric values so 10 does not come before 2, while -r reverses the order. Combine them as sort -nr scores.txt to sort numbers from largest to smallest.
wccount lines, words, bytes
sortsort text or numbers
uniqprocess adjacent duplicates
cutextract fields or characters
Handle duplicates with uniq
uniq compares adjacent lines only, so input is usually sorted first: sort names.txt | uniq. uniq -c prefixes every unique value with its frequency. Without sorting, duplicates separated elsewhere in the file are not combined.
Extract fields with cut
For colon-delimited data, cut -d ":" -f 1 /etc/passwd extracts the first field. -d sets the delimiter and -f selects fields; -f 1,3 selects more than one.
Complete pipeline: cut -d ":" -f 2 users.txt | sort | uniq -c | sort -nr extracts the second field, counts each value, then ranks frequencies from highest to lowest.
Build a City-Frequency Pipeline
users.txtalice:Shanghaibob:Beijingcarol:Shanghaidave:Shenzhenerin:Beijingfrank:ShanghaiKNOWLEDGE QUIZ
KNOWLEDGE QUIZ
Analyze a Team Roster
users.txt uses the format “username:role:city.” Complete four concrete tasks using this section's tools.
- Count the total number of records.
- Extract all usernames and sort them alphabetically.
- List the unique roles.
- Count users per city and rank the counts from highest to lowest.