πŸ“‘Linux/Unix: File Viewing, Redirection & Filters
Estimated reading: 3 minutes 116 views

βœ‚οΈ 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
OptionMeaning
-cCharacter position/column
-dField delimiter
-fField 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:

  • nl adds line numbers to files.
  • wc provides counts of lines, words, and characters.
  • cut extracts specific fields or columns.
  • paste joins files side-by-side.
  • split divides 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 :
Share

πŸ”΅ Linux/Unix: Text Utilities (nl, wc, cut, paste, split)

Or Copy Link

CONTENTS
Scroll to Top