Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / The Linux Command Line

    Text-Processing Tools

    uniq without sort first gives wrong counts, because it only merges adjacent duplicates.

    Chapter 6 of 127 min read

    The Plain Picture#

    The machines on the factory line from Chapter 5: a sieve (keep only certain lines), a sorter, a counter, a slicer (keep only certain columns), a find-and-replace stamp.

    The Real Names#

    ToolJobExample
    grepkeep lines matching a patterngrep "ERROR" app.log
    sortsort linessort -n (numbers), -r (reverse), -u (unique)
    uniqcollapse repeated adjacent lines; -c counts themsort | uniq -c
    cuttake columns by a separatorcut -d',' -f2 data.csv
    awka small language for column-based textawk '{print $1}' (first column)
    sedfind-and-replace in a streamsed 's/http:/https:/g'
    findsearch for files by name, type, size, agefind /var/log -name "*.log" -mtime +7
    xargsturn input lines into arguments of another commandfind . -name "*.tmp" | xargs rm
    jqpick apart JSON (Networking 3: DNS, HTTP and HTTPS Ch 6)curl -s api/notes | jq '.[0].title'

    grep options you'll use daily#

    Terminal
    grep -i "error" app.log         # ignore upper/lower case
    grep -n "error" app.log         # show line numbers
    grep -r "DATABASE_URL" .        # search every file under this folder (recursive)
    grep -v "healthcheck" app.log   # INVERT: lines that do NOT match
    grep -E "401|403" app.log       # extended patterns: 401 OR 403
    grep -A 5 "Traceback" app.log   # also show 5 lines After each match (-B = before)

    The patterns grep, sed and awk use are regular expressions (regex) — a mini-language for text patterns: . any character, * repeat the previous thing zero or more times, ^ start of line, $ end of line, [0-9] any digit. ^ERROR = lines starting with ERROR.

    Under the Hood — a real log analysis#

    StudyBuddy's web server writes one line per request. A sample access.log:

    Output
    49.207.1.10 - - [28/Sep/2026:10:00:01 +0530] "GET /notes/priya HTTP/1.1" 200 1532
    103.21.4.7 - - [28/Sep/2026:10:00:02 +0530] "POST /login HTTP/1.1" 401 52
    49.207.1.10 - - [28/Sep/2026:10:00:03 +0530] "GET /quiz/12 HTTP/1.1" 200 877
    157.48.9.3 - - [28/Sep/2026:10:00:03 +0530] "GET /notes/rahul HTTP/1.1" 500 21
    103.21.4.7 - - [28/Sep/2026:10:00:04 +0530] "POST /login HTTP/1.1" 401 52
    103.21.4.7 - - [28/Sep/2026:10:00:05 +0530] "POST /login HTTP/1.1" 401 52
    49.207.1.10 - - [28/Sep/2026:10:00:06 +0530] "GET /notes/priya HTTP/1.1" 304 0
    157.48.9.3 - - [28/Sep/2026:10:00:07 +0530] "GET /notes/rahul HTTP/1.1" 500 21

    awk splits each line on spaces, numbering the fields $1, $2, …: $1 is the client IP and $9 is the status code (Networking 3: DNS, HTTP and HTTPS Ch 5).

    "Which IPs send the most requests?" — the classic interview question:

    Terminal
    awk '{print $1}' access.log | sort | uniq -c | sort -rn | head -3

    Real output:

    Output
       3 49.207.1.10
       3 103.21.4.7
       2 157.48.9.3

    Read the pipeline stage by stage:

    1. awk '{print $1}' — keep only the IP column.

    2. sort — put identical IPs next to each other (uniq only merges adjacent lines — that's why sort comes first).

    3. uniq -c — collapse duplicates, with a count in front.

    4. sort -rn — sort by that count, as a number, in reverse (biggest first).

    5. head -3 — keep the top 3.

    "How many requests got each status code?"

    Terminal
    awk '{print $9}' access.log | sort | uniq -c | sort -rn
    Output
       3 401
       2 500
       2 200
       1 304

    Three 401s from 103.21.4.7 in 3 seconds against /login looks like someone guessing passwords — that's how the command line helps you spot an attack. Two 500s on /notes/rahul point to a specific bug.

    More recipes:

    Terminal
    grep '" 5[0-9][0-9] ' access.log | wc -l          # count 5xx responses → 2
    find /var/log -name "*.log" -size +100M            # log files bigger than 100 MB
    find . -name "*.pyc" -delete                       # delete all compiled Python files here
    sed -i.bak 's/DEBUG=true/DEBUG=false/' .env        # edit a file in place, keeping a .bak copy
    curl -s https://api.github.com/repos/python/cpython | jq '.stargazers_count'   # one field from JSON

    Gotchas

    • uniq without sort first gives wrong counts, because it only merges adjacent duplicates.

    • sed -i differs between macOS and Linux. On Linux, sed -i 's/a/b/' f works; macOS needs an argument after -i (sed -i '' 's/a/b/' f). Giving a backup suffix like -i.bak works on both.

    • xargs breaks on file names with spaces. Use find ... -print0 | xargs -0 ..., or find ... -exec cmd {} +.

    Check yourself

    4 questions
    1

    Write a pipeline for "the 5 most common status codes in access.log".

    Show answer

    awk '{print $9}' access.log | sort | uniq -c | sort -rn | head -5.

    2

    Why must sort come before uniq -c?

    Show answer

    uniq only merges duplicates that are next to each other; sorting groups them.

    3

    How do you show the 3 lines before and after every "Traceback" in a log?

    Show answer

    grep -B 3 -A 3 "Traceback" app.log (or grep -C 3).

    4

    How do you find files under /tmp older than 7 days?

    Show answer

    find /tmp -type f -mtime +7.