I transformed text non-interactively with sed substitutions for find-replace and awk for field-based reporting and arithmetic. These tools edited config files in place and produced column summaries from logs without ever opening an editor.

Objective & Context

sed and awk are the scripting backbone of text automation. This lab uses sed for line-oriented edits and awk's record/field model for computation, the manual engine behind config templating and log reporting.

Environment & Prerequisites

  • Linux with GNU sed and gawk.
  • A config file and a delimited log.
  • Regex familiarity from the grep lab.

Step-by-Step Execution

1. In-place substitution with sed

sed -i.bak 's/^#Port 22/Port 2222/' /etc/ssh/sshd_config

2. Sum a column with awk

awk -F, '{sum+=$3} END{printf "total: %.2f\n", sum}' sales.csv

3. Conditional field report

awk '$9 >= 500 {print $1, $9}' access.log
total: 18423.50
203.0.113.45 503

Validation & Testing

Confirm sed created a .bak backup and changed only the intended line, and that awk's totals match a manual sum. Pass criteria: targeted in-place edit with backup, correct field math, and accurate conditional filtering.

Advanced: Troubleshooting
  • sed changed too much: anchor the pattern; unanchored matches hit every line.
  • No in-place backup: use -i.bak to keep the original.
  • awk wrong field: set the field separator with -F to match the data.

Key Results

  • Edited config files in place with safe backups and anchored patterns.
  • Computed column totals and reports directly from delimited data.
  • Filtered records by field conditions without a full script.
  • Replaced manual editing for repeatable text transforms.