πŸ“‘Linux/Unix: File Viewing, Redirection & Filters
Estimated reading: 3 minutes 438 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