🚧 Under construction — VoTechWorks is being built. A free tech program — complimentary, as our commitment to get America working.
voTechWorksGrade 13 · a vocational technology learning center, by urbanicity
newSchoolWorks.com › VoTechWorks › Technology & Design › Web & Software Development › Linux/Bash for Web & Software Development › Lesson 4
TEC-WEB46-06 · Lesson 4 · 14 clock hours · lab

Text Processing: grep, sed, awk, and Pipelines

Objective. Students will extract, transform, and summarize data from log and config files using grep, sed, awk, sort, uniq, and pipes.

⚠ Pending professional review

This lesson, its worked examples and its practice questions has not yet been verified by an industry-certified professional or by a certification or approval body. It is a study draft and may contain errors. Do not use it as instructions for work on a job site or on live equipment. Always follow your instructor, your supervising professional, and current official references and regulations.

VoTechWorks program approval and accreditation are still in progress. Credentials are issued only by the certifying bodies named in each program, after their own exams.

Text Processing: grep, sed, awk, and Pipelines

Why Text Processing Matters

Web servers, applications, and system services write their activity to plain-text log and configuration files. Being able to extract, transform, and summarize that text at the command line is one of the most practical skills a Linux-based developer or administrator can have. Instead of opening a 50,000-line log file in a text editor, you write a short pipeline that answers a specific question in seconds.

The Linux Pipeline Model

Linux follows the Unix philosophy: small, single-purpose tools that communicate through standard input (stdin) and standard output (stdout). The pipe character `|` connects tools so the output of one becomes the input of the next. This lets you build powerful one-liners without writing a full script.

```

command1 | command2 | command3

```

Data flows left to right through the pipeline. Nothing is written to disk unless you redirect the final output with `>` (overwrite) or `>>` (append).

grep — Search and Filter

`grep` scans lines of text and prints only those that match a pattern.

Regular expressions (regex) are patterns that describe sets of strings. Common metacharacters: `.` (any character), `*` (zero or more of the preceding), `^` (start of line), `$` (end of line), `[0-9]` (character class).

sed — Stream Editor

`sed` reads lines one at a time, applies editing commands, and writes the result to stdout. It is most commonly used for search-and-replace.

Safety note: The `-i` flag rewrites the original file permanently. Always keep a backup copy of any production configuration or log file before running an in-place substitution. A common practice is `sed -i.bak 's/old/new/g' file`, which saves the original as `file.bak` before making changes.

awk — Pattern-Action Processing

`awk` reads input line by line, splits each line into fields, and lets you act on specific fields. By default, fields are separated by whitespace and are referenced as `$1`, `$2`, `$3`, and so on. `$0` refers to the entire line.

Apache's combined log format places the client IP address in field 1 and the HTTP status code in field 9, which makes `awk` a natural fit for log analysis.

sort and uniq — Counting and Ranking

Combining `sort | uniq -c | sort -rn` produces a ranked frequency table — exactly what you need to find the IP addresses generating the most 404 errors.

Saving Output

Redirect the final output of any pipeline to a file:

```

grep "404" access.log | awk '{print $1}' | sort | uniq -c | sort -rn > top_404_ips.txt

```

Use `>>` to append to an existing file rather than overwriting it.

Reading man Pages

Every tool covered here has a manual page. Run `man grep`, `man sed`, or `man awk` to read it. Press `q` to quit. The SYNOPSIS section shows the syntax; the OPTIONS section explains each flag. Consulting the man page before guessing at a flag is a professional habit.

Good Practices in the Lab

Key terms9

pipelineA chain of commands connected by the pipe character `|` so that the stdout of one command feeds directly into the stdin of the next.
grepA command-line utility that searches text line by line and prints only the lines matching a given pattern or regular expression.
sedA stream editor that reads input line by line and applies editing commands such as search-and-replace; the `-i` flag edits a file in place.
awkA text-processing tool that splits each input line into fields and applies pattern-action rules; commonly used to extract specific columns from structured text.
regular expression (regex)A sequence of characters that defines a search pattern, used by grep, sed, and awk to match strings with metacharacters such as `.`, `*`, `^`, and `$`.
uniq -cAn option that collapses consecutive duplicate lines and prepends each unique line with the number of times it appeared; requires sorted input to work correctly.
stdout / stdinStandard output (stdout) is the default destination for a command's results; standard input (stdin) is where a command reads data. Pipes connect one command's stdout to the next command's stdin.
field (awk)One of the whitespace-delimited tokens on an input line as seen by awk; referenced as $1, $2, etc., with $0 representing the entire line.
redirectionUsing `>` or `>>` to send a command's output to a file instead of the terminal; `>` overwrites the file and `>>` appends to it.
In class: Guided practice: instructor walks through parsing an Apache access.log sample — students use grep to filter 404 errors, pipe to awk to extract IP addresses, sort and uniq -c to count frequency, then sort -rn to rank by count. · Independent lab: students are given a 500-line application log and a task sheet requiring six specific queries (e.g., count POST requests, extract timestamps for errors, replace deprecated config key with new key using sed); results are saved to an output file. · Peer review: partners exchange output files and verify results by running the same queries independently, resolving discrepancies together and noting them in their lab logs.
Materials: Sample Apache access.log and application log files (provided), grep/sed/awk man pages, lab task sheet, student VMs
Check: Instructor compares student output files to answer key; 5 of 6 queries must produce correct output to pass.

Practice check8

Answer each question, then check it. Nothing is recorded; this is your own practice.

Linux command-line tools and file management
1. Which command prints only the lines in access.log that contain the string "404"?
Linux command-line tools and file management
2. A developer wants to replace every occurrence of the deprecated key "db_host" with "database_host" in the file config.ini without creating a new file. Which command is correct?
Linux command-line tools and file management
3. In Apache's combined log format, the client IP address is in field 1 and the HTTP status code is in field 9. Which awk command prints only the IP addresses of requests that returned a 404 status?
Linux command-line tools and file management
4. A student runs the pipeline: `grep "404" access.log | awk '{print $1}' | uniq -c | sort -rn`. The output does not show the correct frequencies. What is the most likely cause?
Linux command-line tools and file management
5. Which operator would you use to add the output of a command to the end of an existing file without deleting its current contents?
Linux security and safe system administration practices
6. Before running `sed -i 's/OldKey/NewKey/g' production.conf` on a live server, what is the safest precaution?
Linux command-line tools and file management
7. Which grep flag makes a pattern search case-insensitive so that "Error", "ERROR", and "error" all match?
Linux command-line tools and file management
8. A student needs to count the total number of POST requests in access.log. Which single command accomplishes this most directly?
Record this lesson on my transcript. Sign in with your key and enrollment email to record lessons (enroll free if you have no key). This page stays open to read and practise.