Skip to main content
Albero Academy

Files, Pipes & Text Processing

The Unix superpower — combining small tools with pipes and redirection, and searching text with grep, sort and friends.

Chapter 2Bash15 min readBeginner friendly
  • #Bash
  • #Pipes
  • #grep

This is where the shell becomes powerful. The Unix philosophy is to build small tools that each do one thing well, then combine them. Pipes are the glue — the output of one command becomes the input of the next — and with them you'll process text and data in one line that would take a whole script elsewhere.

The Unix philosophy

Each command reads input, does one job, and writes output. Because they all speak plain text, any command's output can feed any other's input. Master a handful of small tools and you can combine them endlessly.

Redirection

By default, output goes to the screen. Redirect it to a file instead:

redirecting output & input
ls > files.txt # write output to a file (overwrites)
ls >> files.txt # append to a file
sort < names.txt # feed a file in as input
command 2> err.log # redirect errors (stderr) to a file

Pipes

The pipe | connects commands. Read it left-to-right as "then": list the files, then search them, then count:

composing with pipes
# Count how many .log files are here
ls | grep ".log" | wc -l
# Find the 5 largest processes by memory
ps aux | sort -k4 -r | head -5
# Unique programmes, sorted, in a CSV column
cat students.csv | cut -d, -f2 | sort | uniq -c

Text tools

ToolDoes
grepFind lines matching a pattern
cutExtract columns/fields
sortSort lines
uniqCollapse or count duplicates
wcCount lines, words, characters
head / tailFirst / last N lines
sedFind-and-replace / stream edit
awkField-based processing & sums
grep, sed & awk
grep -i "error" app.log # case-insensitive search
grep -rn "TODO" src/ # recursive, with line numbers
sed 's/old/new/g' file.txt # replace every "old" with "new"
awk -F, '{ sum += $3 } END { print sum }' data.csv # sum column 3

Practice

Given a CSV, build a one-liner pipeline that extracts one column, sorts it, and counts how many times each value appears (cut | sort | uniq -c). Then use grep to find all error lines in a log and wc -l to count them.

Key takeaways

  1. 01The Unix philosophy: small single-purpose tools that combine via plain text.
  2. 02Redirect output with > (overwrite) and >> (append); feed input with <.
  3. 03Pipes (|) chain commands — one's output becomes the next's input.
  4. 04Core text tools: grep, cut, sort, uniq, wc, head/tail, sed, awk.
  5. 05tail -f follows logs live; pipe into grep to filter to what matters.