CyberStars

Find & Replace: sed

The sed command (stream editor) performs text transformations on the fly. Its most common use is find and replace:

sed 's/old/new/' file.txt

The s stands for substitute. This replaces the first occurrence of old with new on each line.

Replace all occurrences on each line with g

sed 's/error/WARNING/g' log.txt

The trailing g (global) replaces every match on the line, not just the first.

sed does not modify the original file

By default, sed prints the result to stdout. The file stays unchanged. To save, use redirection:

sed 's/old/new/g' data.txt > data_fixed.txt

Delimiters

The / is conventional but you can use any character:

sed 's|/usr/bin|/opt/bin|g' paths.txt

Using | avoids escaping all those slashes.


Mission: Inspection Report Fix

The station passed its safety re-inspection: every test that failed has been run again and passed. report.txt still shows the old results, and the inspection rules say a retested result reads PASS (retest), so the delegation can tell which ones were run twice. Save the corrected report as records/report-clean.txt, made from report.txt, not typed again.

Expected result

records/report-clean.txt has every test that had failed marked PASS (retest), the others as they were, and no FAIL left.