SI Learning
0% read · Practice is tracked separately
← My learning
Version 1 · Chapter 6Reading mode

Turn text into answers with pipelines

By the end: Combine small tools to clean, group, filter, and summarize data.

The idea

A pipe connects one command's standard output to the next command's standard input. It does not normally include error output. Build a pipeline one stage at a time and inspect the intermediate result. These examples use tiny, deliberately simple records; CSV with quoted commas needs a CSV parser.

Your command toolkit

sort

What it solves: Put records into a useful order.

bash
printf '%s\n' 12 3 8 | sort -n

-n sorts numerically. Plain sort is textual and its collation depends on locale; set LC_ALL=C for reproducible byte-oriented sorting.

uniq

What it solves: Count adjacent repeated records.

bash
printf '%s\n' api web api | sort | uniq -c

uniq only merges adjacent duplicates, so sorting first matters. The counts tell you how often each value occurred.

cut

What it solves: Extract a simple delimited field.

bash
printf 'api,200\nweb,500\n' | cut -d, -f2

Selects the second comma-delimited field. It does not understand quoted CSV fields or nested formats.

paste

What it solves: Join records side by side.

bash
paste names.txt scores.txt

Joins corresponding lines, normally with a tab. Run the lab first to create these two files.

tr

What it solves: Translate or remove characters.

bash
printf 'READY\n' | tr '[:upper:]' '[:lower:]'

tr works on a stream rather than accepting a filename argument. Character classes make intent clearer than hardcoded alphabets.

sed

What it solves: Apply a simple text transformation.

bash
printf 'stage=draft\n' | sed 's/draft/review/'

Prints the transformed stream. No -i means the source is untouched, making this a useful preview before file edits.

awk

What it solves: Filter records and calculate fields.

bash
printf 'api 12\nweb 8\n' | awk '{total += $2} END {print total}'

awk splits records into fields. Here it accumulates the second field and prints 20 at the end.

column

What it solves: Make a plain text table readable.

bash
printf 'NAME SCORE\nAsha 9\nJuma 8\n' | column -t

Aligns whitespace-separated fields for a human reader. Keep the original data format for scripts.

xargs

What it solves: Turn a stream of filenames into arguments.

bash
find . -maxdepth 1 -type f -print0 | xargs -0 -r wc -l

NUL delimiters preserve spaces and newlines in filenames. GNU -r avoids invoking the command when there is no input.

Before you move on

Try the guided lab below. Compare the result with the explanation, then record your reasoning in the workbook. A missing optional program or a restricted environment is something to explain, not a reason to change system settings blindly.

Practice with evidence

  1. Predict the result
  2. Run the guided lab
  3. Compare the output
  4. Explain your choice

A worked example

Guided lab

bash
si_lab=$(mktemp -d)
cd "$si_lab" || exit
printf 'Asha\nJuma\n' > names.txt
printf '9\n8\n' > scores.txt
paste names.txt scores.txt
printf 'api\nweb\napi\nworker\nweb\napi\n' | sort | uniq -c | sort -nr

What to expect: Two name/score rows, followed by api with 3 occurrences, web with 2, and worker with 1.

Make it yours: Explain every stage of the frequency-count pipeline. Change the sample records and predict the new counts before running it.

Read further

Apply what you learned

Your private practice

Why does sort usually come before uniq when counting duplicates?

Answers are private. Completion requires the correct knowledge-check answer and a written response when an activity is included.

Restore a downloaded workbook

Choose a recovery file for this course and version. Review the recovered copy before saving it to your account.

Ask a question or report a problem

Describe what needs attention. Your private answer is shared only if you select the option below.

All learning paths
SI CENTRALIZATION · LEARN

Linux Command Line: from first command to confident practice

24 chapters · Your own pace

0 of 24 chapters complete
Turn text into answers with pipelines | SI Centralization