Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
FREEDOM251

Freedom251 › Blog › Command Line

Why sed Replaced Four Matches Instead of One—and How to Preview the Fix

By Freedom251 Team · · 3 min read

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

sed 's/port/listen_port/g' app.yml replaces every occurrence of the letters port on each line, including occurrences inside longer names and paths. To target a standalone word with GNU sed, add word boundaries; before changing a file in place, preview the transformed output and inspect a diff.

Why the command changed four lines

In s/port/listen_port/g, port is an unanchored regular-expression pattern. It can match those four letters wherever they occur, not just when they form a separate word. The trailing g asks sed to replace every match on each line.

As an Amazon Associate I earn from qualifying purchases.

Anguishe’s October 1, 2026 DEV Community article describes a YAML example with port: 8080, support_email: [email protected], transport: tcp, and report_dir: /var/reports. The article reports that sed -i 's/port/listen_port/g' app.yml changed all four lines. That is an author-reported example: the pattern also matches the port substring in each longer name or path.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

This is not sed guessing incorrectly; it follows the pattern it was given. A successful exit status indicates that the command ran, not that the resulting text matches your intention.

How to match standalone port with GNU sed

GNU sed 4.10 supports b for a word boundary. Use it on both sides of the pattern:

sed 's/bportb/listen_port/g' app.yml

GNU sed defines word characters as letters, digits, and underscores. That means port in db_port is not a standalone word under this rule, because the underscore counts as a word character. The pattern will match port when it is separated from neighboring characters by a boundary, such as whitespace or punctuation.

b is GNU-specific rather than portable POSIX sed syntax. Boundary and in-place editing options vary among sed implementations, so check the manual for the sed actually used—especially in scripts that must run on multiple platforms.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Preview the result before writing it

A safer workflow is to transform standard output first, compare it with the original, and only then decide whether to edit the file. Anguishe’s article gives this preview command:

sed 's/bportb/listen_port/g' app.yml | diff app.yml -

Here, diff compares the original file with the transformed output from standard input (the - argument). Review the displayed changes and confirm that only the intended value changed before running an in-place edit. A diff exit status of 1 ordinarily means differences were found; it is not by itself proof that sed failed.

For the original unbounded pattern, the same preview technique would expose the unwanted matches before they reached disk. Treat preview-and-review as a separate safety step, not as a replacement for writing a precise pattern.

Choose the edit method that matches the data

Approach Match scope Portability and review
s/port/listen_port/g Every occurrence of the substring port on each line, including inside larger identifiers or paths. Simple, but too broad for a standalone-word change. Preview and inspect a diff before any in-place edit.
s/bportb/listen_port/g Standalone port according to GNU sed’s word-character rules; underscores count as word characters. b is GNU-specific. Check the target implementation and preview the output.
A format-aware parser Can target a YAML or JSON field by its structure rather than matching text wherever it appears. Prefer this when the intended change depends on which key or value is being edited. Use a YAML parser for YAML or a JSON-aware tool such as jq for JSON.

Plain sed processes input line by line, which makes it useful for many text substitutions. GNU sed can also put multiple lines into its pattern space with commands such as N, so it is not accurate to say sed can never work across newlines. But when an edit depends on YAML or JSON structure, a parser that understands that format is generally the clearer choice.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Check the replacement text as well as the pattern

sed gives some characters special meaning on the replacement side too. An unescaped & inserts the entire text matched by the pattern; to put a literal ampersand in the replacement, escape it as &. This behavior is specified by both the GNU sed manual and POSIX. If a pattern or replacement contains many slashes, sed also allows a different single-character delimiter, which can make the expression easier to read.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Command LineGNU sedLinuxsedtext processing

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.