βοΈ Linux/Unix: Text Utilities β nl, wc, cut, paste, split Explained with Examples
π§² Introduction β Why Learn Linux Text Utilities?
Linux/Unix provides powerful command-line tools for processing and transforming text files. Whether you need to count lines, extract columns, or split files, utilities like nl, wc, cut, paste, and split let you manipulate data efficiently and script with ease.
π― In this guide, youβll learn:
- How to number lines, count words, extract fields, and merge or split files
- Command syntax and real-world examples
- Use cases for automation, data processing, and scripting
π’ nl β Number Lines in a File
β Syntax:
nl [options] filename
π Description:
Adds line numbers to each line in a text file (default starts at 1).
π§ͺ Example:
nl notes.txt
π‘ Output:
1 This is the first line.
2 Second line here.
β
Use -ba to number all lines, including blank ones:
nl -ba file.txt
π€ wc β Word, Line, Character Count
β Syntax:
wc [options] filename
π Description:
Counts lines, words, characters, or bytes in a file.
π§ͺ Examples:
wc file.txt # Show all counts
wc -l file.txt # Count lines
wc -w file.txt # Count words
wc -c file.txt # Count bytes
π‘ Output:
10 45 302 file.txt
(Line, Word, Byte counts)
βοΈ cut β Extract Fields or Columns
β Syntax:
cut [options] filename
π Description:
Extracts specific columns or fields from a file.
π§ͺ Examples:
Extract first 10 characters of each line:
cut -c 1-10 file.txt
Extract the first field (column) using : as a delimiter:
cut -d ':' -f 1 /etc/passwd
| Option | Meaning |
|---|---|
-c | Character position/column |
-d | Field delimiter |
-f | Field number(s) |
π§© paste β Merge Files Side-by-Side
β Syntax:
paste [options] file1 file2
π Description:
Merges lines of files horizontally, joining content with a tab (default).
π§ͺ Example:
File 1: names.txt
Alice
Bob
File 2: scores.txt
85
92
paste names.txt scores.txt
π‘ Output:
Alice 85
Bob 92
β
Use -d to change the delimiter:
paste -d ',' file1 file2
πͺ split β Break a File into Smaller Files
β Syntax:
split [options] filename [prefix]
π Description:
Splits a large file into smaller chunks (lines or bytes).
π§ͺ Examples:
Split by 10 lines each:
split -l 10 bigfile.txt part_
This creates files like part_aa, part_ab, …
Split by size (e.g., 1MB chunks):
split -b 1M largefile.zip segment_
β Great for handling big log files or archiving.
π Summary β Recap & Next Steps
Linux text utilities give you precise control over how you read, format, combine, and split text files. They are especially useful in scripting, data pipelines, and terminal workflows.
π Key Takeaways:
nladds line numbers to files.wcprovides counts of lines, words, and characters.cutextracts specific fields or columns.pastejoins files side-by-side.splitdivides large files into manageable chunks.
β FAQs
β How do I get the first column from a CSV file?
β
Use:
cut -d ',' -f1 file.csv
β Can I combine cut and paste?
β
Yes! You can extract fields from multiple files and paste them together:
paste <(cut -f1 file1) <(cut -f2 file2)
β How do I split a file every 1000 lines?
β
Use:
split -l 1000 largefile.txt chunk_
β What’s the difference between nl and cat -n?
β
Both add line numbers, but nl offers more control over formatting and blank lines.
β Can I number only non-empty lines with nl?
β
Yes. Thatβs the default behavior of nl. Use -ba to number all lines.
Share Now :
