Skip to main content
RunBook Academy

LinuxII · Shell and Command-Line OperationsText tools

Core text tools — grep, cut, sort, uniq, tr, wc

Foundation⏱ ~12 min🧪 Lab requiredbashgrepsortuniqcuttrwc

What you'll learn

  • Use grep with -E, -F, -i, -v, -c, -l, -n, -o, and -P correctly
  • Use cut, sort, uniq, tr, and wc in production-style pipelines
  • Choose the right tool for the structure of the input
  • Identify when to reach for awk or sed instead

Prerequisites

Verified against Ubuntu 24.04 LTS · Debian 12 (Bookworm) · RHEL 9.x · Rocky Linux 9.x · AlmaLinux 9.x · Linux kernel 6.1 LTS / 6.6 LTS · systemd 255+ · OpenSSH 8.7p1 (RHEL 9) / 9.6p1 (Ubuntu 24.04) · nftables 1.0.x · chrony 4.x · Pacemaker 2.1.x · Corosync 3.1.x · 2026-08-09

Not yet marked complete on this device.

Six text tools cover 80% of production log-and-data triage. This lesson is the muscle-memory foundation that the next two lessons (sed, awk; find, xargs) build on.

grep — find lines that match

The most-used flags in production:

FlagEffect
-iCase-insensitive
-vInvert: print non-matching lines
-cCount matches per file
-lList filenames that have at least one match
-nPrint line numbers
-oPrint only the matched text, not the whole line
-EExtended regex (egrep)
-FFixed string, no regex
-PPerl-compatible regex (PCRE) — fastest for complex patterns
-A NPrint N lines of context after each match
-B NPrint N lines of context before each match
-C NPrint N lines of context around each match
--color=autoHighlight matches in interactive use
-rRecurse into directories
-RLike -r but follows symlinks
Read-only / Safegrep with line numbers
$ grep -n 'systemd' /etc/group | head -3
42:systemd-journal:x:101:
43:systemd-network:x:102:
44:systemd-resolve:x:103:

Illustrative output

cut — slice by column

cut extracts columns from delimited text. Three modes:

Read-only / Safecut by delimiter
$ grep '^root:' /etc/passwd | cut -d: -f1,7
root:/bin/bash

Illustrative output

Read-only / Safecut by character position
$ uname -a | cut -c1-10
Linux 6.6.

Illustrative output

sort — order lines

Read-only / Safesort by numeric UID
$ grep '^root:' /etc/passwd; printf '\n'; grep -v '^root:' /etc/passwd | sort -t: -k3 -n | head -3
root:x:0:0:root:/root:/bin/bash

abrt:x:173:173::/etc/abrt:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
avahi:x:120:124:Avahi mDNS daemon:/run/avahi:/sbin/nologin

Illustrative output

Useful flags: -r reverse, -u unique (sort + dedupe), -h human numeric sort (-h understands 1K, 2G), -V version sort, -R random, -s stable (disable last-resort comparison).

uniq — collapse adjacent duplicates

Read-only / Safeuniq counts
$ printf 'a\nb\nb\nc\nb\n' | uniq -c
      1 a
2 b
1 c
1 b

Illustrative output

tr — translate or delete characters

Read-only / Safetr uppercase
$ echo "Hello, World!" | tr 'a-z' 'A-Z'
HELLO, WORLD!

Illustrative output

Read-only / Safetr delete
$ printf 'a\nb\nc\n' | tr -d '\n'; echo
abc

Illustrative output

Read-only / Safetr squeeze
$ echo "hello   world" | tr -s ' '
hello world

Illustrative output

wc — word, line, character, byte count

Read-only / Safewc default
$ wc /etc/passwd
   46   92 3010 /etc/passwd

Illustrative output

The classic top-N pipeline

Many production diagnostics start with this shape:

Read-only / Safetop source IPs
$ journalctl -u ssh --since '1 day ago' --no-pager 2>/dev/null | grep -oE 'from [0-9.]+' | sort | uniq -c | sort -rn | head -10
...

Illustrative output

Knowledge check

Knowledge check · 3 questions

  1. Q1. Which grep flag prints only the matched text, not the entire matching line?

  2. Q2. uniq collapses duplicate lines wherever they appear in the input.

  3. Q3. Which of the following patterns correctly sort /etc/passwd by numeric UID? Select all that apply.

Passing score: 75%. Answers are checked in this browser.