Working with Text Files | wc, sort, uniq & cut | Mac/Linux Terminal
Video: Working with Text Files | wc, sort, uniq & cut | Mac/Linux Terminal by Taught by Celeste AI - AI Coding Coach
Watch full page →Working with Text Files | wc, sort, uniq & cut | Mac/Linux Terminal
Learn how to efficiently analyze and manipulate text files using essential terminal commands on Mac and Linux. This guide covers counting lines, words, and characters with wc, sorting and identifying unique lines with sort and uniq, and extracting specific columns from structured text using cut.
Code
# Count lines, words, and characters in a file
wc filename.txt
# Count only lines
wc -l filename.txt
# Count only words
wc -w filename.txt
# Count only characters (including spaces and newlines)
wc -c filename.txt
# Count multibyte characters correctly
wc -m filename.txt
# Count number of files in current directory
ls | wc -l
# Sort lines alphabetically
sort filename.txt
# Sort lines numerically
sort -n numbers.txt
# Sort lines in reverse (descending) order
sort -r filename.txt
# Find unique lines in a sorted file
sort filename.txt | uniq
# Count occurrences of unique lines
sort filename.txt | uniq -c
# Show only duplicate lines
sort filename.txt | uniq -d
# Show only unique lines that appear once
sort filename.txt | uniq -u
# Extract the first field from a colon-separated file (e.g., /etc/passwd)
cut -d ':' -f 1 /etc/passwd
# Extract multiple fields (1 and 3) from a comma-separated file
cut -d ',' -f 1,3 data.csv
# Extract a range of fields (fields 1 to 3)
cut -d ',' -f 1-3 data.csv
# Extract characters 1 through 10 from each line
cut -c 1-10 filename.txt
Key Points
wccounts lines, words, and characters, with flags to customize output for specific counts.sortorganizes text lines alphabetically or numerically, and can reverse order with-r.uniqworks on sorted input to identify unique, duplicate, or counted lines for text analysis.cutextracts specific fields or character ranges from structured text using delimiters and field numbers.- Combining these commands in pipelines enables powerful text processing workflows in the terminal.