sed: Editing Text Streams
How sed processes text line by line, and how to use substitution, addresses, ranges and safe in-place editing to change config files and logs from scripts.
By The Dark DebuggerUpdated 24 min read
sed is the stream editor: it reads text, applies editing commands to each line, and
writes the result, with no editor window and no human in the loop. This article
explains how sed actually processes input, then covers substitution, regular
expressions with capture groups, addresses and ranges, the other editing commands, and
in-place editing done safely. It assumes you know grep and basic regular expressions
from searching and filtering text; the next article
covers awk for column-oriented work.
Why sed
Picture a provisioning script that installs a service on forty servers. Each server
needs the same config file with two values changed: a different database host and a
larger connection pool. You can't open an editor inside a Dockerfile RUN step, a CI
job or a script running over SSH. You need a command that says "in this file, change
this to that" and does it the same way every time.
That is sed. It is installed on every Unix-like system, it handles files of any size
because it streams them a line at a time, and one-line sed commands are a staple of
Dockerfiles, CI pipelines and install scripts.
Most examples use this config file. Save it as app.conf:
# app.conf - order service configuration
# Managed by provisioning; local edits may be overwritten.
[server]
host = 0.0.0.0
port = 8080
workers = 4
# tls_cert = /etc/ssl/certs/orders.pem
[database]
host = db01.internal
port = 5432
name = orders
user = orders_app
max_connections = 50
[cache]
host = cache01.internal
port = 6379
[metrics]
enabled = true
port = 9100Note that host and port appear in several sections. That is deliberate: changing the
right one is most of the skill. A few examples also use the access.log, app.log and
orders.csv files from the previous article.
How sed works: the cycle
sed runs a simple loop, called a cycle, once per input line:
- Read the next line into a buffer called the pattern space (without its newline).
- Run every command in the script, in order, against the pattern space.
- Print the pattern space (unless
-nwas given), then start the next cycle.
That's the whole model. An empty script prints every line unchanged:
printf '%s\n' web01 web02 | sed ''web01
web02The p command prints the pattern space. Because step 3 also prints it, every line
appears twice:
printf '%s\n' web01 web02 | sed 'p'web01
web01
web02
web02-n turns off the automatic print in step 3, so only explicit p commands produce
output. sed -n plus p is how you use sed as a filter:
printf '%s\n' web01 web02 | sed -n 'p'
sed -n '6p' app.confweb01
web02
port = 8080Two consequences of the model are worth holding onto. First, sed never sees the
whole file at once, so it works on a 20 GB log as easily as on a 20-line config.
Second, sed does not change the input file unless you ask it to with -i; by default
it writes to stdout and you redirect.
Substitution: s/pattern/replacement/flags
The s command is the one you'll use most. It finds text matching a regular
expression and replaces it. By default it replaces only the first match on each
line:
echo "upstream db01.internal:5432; # was db01.internal:5433" | sed 's/db01/db02/'upstream db02.internal:5432; # was db01.internal:5433Flags after the last delimiter change that:
| Flag | Effect |
|---|---|
g | replace every match on the line |
N (a number) | replace only the Nth match |
Ng | replace the Nth match and every one after it (GNU) |
I | match case-insensitively (GNU) |
p | print the line if a substitution happened (use with -n) |
w file | write the line to a file if a substitution happened |
echo "upstream db01.internal:5432; # was db01.internal:5433" | sed 's/db01/db02/g'
echo "2026-11-09,north,keyboard,2" | sed 's/,/;/2'
echo "2026-11-09,north,keyboard,2" | sed 's/,/;/2g'
echo "Error: disk ERROR, error code 5" | sed 's/error/WARN/gI'upstream db02.internal:5432; # was db02.internal:5433
2026-11-09,north;keyboard,2
2026-11-09,north;keyboard;2
WARN: disk WARN, WARN code 5-n with the p flag prints only lines where a substitution happened. It is a neat
way to extract a value: match the line, delete the part you don't want, print what's
left:
sed -n 's/^port = //p' app.conf8080
5432
6379
9100Choosing a delimiter
The / after s is just a delimiter; any character works. When the pattern or
replacement contains slashes, such as file paths and URLs, escaping each one gets
unreadable fast:
echo "root /var/www/shop;" | sed 's/\/var\/www/\/srv\/www/'
echo "root /var/www/shop;" | sed 's|/var/www|/srv/www|'root /srv/www/shop;
root /srv/www/shop;Same result; the second is the one you can read. |, # and , are common choices.
Pick a character that cannot appear in the pattern or the replacement.
The replacement: & and escapes
In the replacement, & stands for the whole matched text:
echo "order 1043 failed" | sed 's/[0-9][0-9]*/#&/'order #1043 failedSo a literal ampersand in the replacement must be written \&, and the same goes for
the delimiter and backslash itself:
echo "R and D" | sed 's/ and / \& /'R & DForgetting this is harmless in a one-liner you can see, and nasty when the replacement comes from a variable; see Where you'll use this.
Regular expressions, groups and back-references
sed uses the same regular expressions as grep: basic (BRE) by default, where
+ ? { } | ( ) need a backslash to be special, and extended (ERE) with -E. Capture
groups make -E worth using almost always. Parentheses capture part of the match, and
\1 to \9 in the replacement insert what each group captured.
Reformat an ISO date as day/month/year, first with -E, then the same thing in BRE:
echo "2026-11-09" | sed -E 's/([0-9]{4})-([0-9]{2})-([0-9]{2})/\3\/\2\/\1/'
echo "2026-11-09" | sed 's/\([0-9]\{4\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\)/\3\/\2\/\1/'09/11/2026
09/11/2026The BRE version shows why -E exists. Swap "Last, First" into "First Last":
echo "Patel, Raj" | sed -E 's/^([^,]+), (.+)$/\2 \1/'Raj Patel[^,]+ ("one or more characters that aren't a comma") is the key idea there. Negated
classes like [^,], [^"] and [^ ] stop a match at the next delimiter, which is
almost always what you want in structured text.
A realistic one: anonymise client IPs in an access log by zeroing the last octet before you share it:
sed -E 's/^([0-9]+\.[0-9]+\.[0-9]+)\.[0-9]+ /\1.0 /' access.log | cut -d' ' -f1 | head -n 4203.0.113.0
203.0.113.0
10.0.0.0
198.51.100.0GNU sed also supports case conversion in the replacement: \U uppercases until \E
or the end, \L lowercases, and \u / \l change only the next character:
echo "region: north" | sed -E 's/: (.*)/: \U\1/'
echo "db01.internal" | sed -E 's/^(.)/\u\1/'region: NORTH
Db01.internalThese are GNU extensions; BSD and macOS sed print a literal U.
Addresses: choosing which lines a command applies to
Every command can be preceded by an address that limits it to certain lines. No address means every line.
| Address | Selects |
|---|---|
6 | line 6 |
$ | the last line |
/regex/ | lines matching the regex |
4,7 | lines 4 to 7 |
/start/,/end/ | from a line matching start through the next line matching end |
5,$ | line 5 to the end |
0~10 | every 10th line (first~step, GNU) |
/regex/,+2 | a matching line and the 2 lines after it (GNU) |
addr! | every line the address does not select |
sed -n '4,7p' app.conf
sed -n '$p' app.conf[server]
host = 0.0.0.0
port = 8080
workers = 4
port = 9100Ranges
A range starts at the first line matching the first address and ends at the next line matching the second, inclusive. A config section runs from its header to the next blank line:
sed -n '/^\[database\]/,/^$/p' app.conf[database]
host = db01.internal
port = 5432
name = orders
user = orders_app
max_connections = 50
The GNU extensions are handy for logs. /ERROR/,+2 shows each error with the two lines
after it, much like grep -A 2, and 0~10 samples every tenth line:
sed -n '/ERROR/,+2p' app.log2026-11-09 08:15:58 ERROR [http-8080-3] order 1043 failed
java.net.SocketTimeoutException: Read timed out
at com.example.orders.PaymentClient.charge(PaymentClient.java:88)
2026-11-09 09:10:44 ERROR [http-8080-2] order 1045 failed
java.sql.SQLTransientConnectionException: orders-pool - Connection is not available
at com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:696)sed -n '0~10p' access.log | cut -d' ' -f1,4192.0.2.15 [09/Nov/2026:08:05:04
10.0.0.5 [09/Nov/2026:08:22:00
198.51.100.7 [09/Nov/2026:10:01:16Two range behaviours catch people out, and both are silent:
- If the end address never matches, the range runs to the end of the input.
- If the start address never matches, the range selects nothing at all.
Both matter when you cut a time window out of a log. \%regex% is an address with %
as its delimiter, which saves escaping the slashes in the dates:
sed -n '\%09/Nov/2026:08:15%,\%09/Nov/2026:08:22%p' access.log | cut -d' ' -f1,4,9198.51.100.23 [09/Nov/2026:08:15:21 201
198.51.100.23 [09/Nov/2026:08:15:58 500
203.0.113.9 [09/Nov/2026:08:20:13 200
203.0.113.9 [09/Nov/2026:08:20:14 200
10.0.0.5 [09/Nov/2026:08:22:00 200That worked because lines with both timestamps exist. Ask for a window starting at 08:16, when no request happened, and you get nothing; end at 08:21, which also never appears, and you get the rest of the file:
sed -n '\%09/Nov/2026:08:16%,\%09/Nov/2026:08:22%p' access.log | wc -l
sed -n '\%08:20:13%,\%08:21%p' access.log | wc -l0
16When the boundaries might not exist, select by pattern instead of by range:
sed -nE '/:08:(1[5-9]|2[0-2]):/p' access.log | cut -d' ' -f1,4,9198.51.100.23 [09/Nov/2026:08:15:21 201
198.51.100.23 [09/Nov/2026:08:15:58 500
203.0.113.9 [09/Nov/2026:08:20:13 200
203.0.113.9 [09/Nov/2026:08:20:14 200
10.0.0.5 [09/Nov/2026:08:22:00 200Addresses with s: changing only the right line
This is where addresses pay off. The [server] port should become 9090, and the three
other port lines must not change. Put a range in front of s:
sed '/^\[server\]/,/^$/ s/^port = .*/port = 9090/' app.conf | grep -n '^port'6:port = 9090
12:port = 5432
19:port = 6379
23:port = 9100! inverts an address. sed '/^\[database\]/,/^$/!d' deletes every line outside the
database section, which prints the same section as the -n ... p version above.
Other commands: d, q, =, a, i, c
s and p are only two of sed's commands. These are the others you'll use:
| Command | Does |
|---|---|
d | delete the pattern space and start the next cycle (the line is not printed) |
q | print the current line and quit |
= | print the current line number |
a text | append a line of text after the current line |
i text | insert a line of text before the current line |
c text | replace the current line (or range) with text |
d is how you drop lines: sed '1d' removes a CSV header, sed '/^#/d' removes comment
lines. q stops reading early, so sed '3q' is head -n 3, and on a huge file it
returns immediately:
sed '1d' orders.csv | head -n 2
sed '3q' orders.csv
sed -n '/ERROR/=' app.log1001,2026-11-01,north,keyboard,2,49.99
1002,2026-11-01,south,monitor,1,189.00
order_id,date,region,product,qty,unit_price
1001,2026-11-01,north,keyboard,2,49.99
1002,2026-11-01,south,monitor,1,189.00
4
11a, i and c add and replace whole lines. Add a ttl setting as the first line of
the [cache] section:
sed '/^\[cache\]/a ttl = 300' app.conf | sed -n '/^\[cache\]/,/^$/p'[cache]
ttl = 300
host = cache01.internal
port = 6379
Insert a comment above a section, and replace a line wholesale:
sed '/^\[metrics\]/i # scraped by Prometheus every 15s' app.conf | tail -n 4
sed '/^workers = /c workers = 8' app.conf | sed -n '/^\[server\]/,/^$/p'# scraped by Prometheus every 15s
[metrics]
enabled = true
port = 9100
[server]
host = 0.0.0.0
port = 8080
workers = 8
# tls_cert = /etc/ssl/certs/orders.pem
The one-line form a text is a GNU extension. POSIX sed, including macOS, needs
a\ followed by a newline and the text; if you write scripts for both, use that form.
Several commands at once
There are three ways to give sed more than one command, and they are equivalent:
several -e options, commands separated by ;, or a script file with -f:
sed -e '/^#/d' -e '/^$/d' app.conf | head -n 3
sed '/^#/d; /^$/d' app.conf | head -n 3[server]
host = 0.0.0.0
port = 8080
[server]
host = 0.0.0.0
port = 8080Braces apply several commands to one address. Repoint the database section at a new server and port in one pass:
sed '/^\[database\]/,/^$/{ s/^host = .*/host = db02.internal/; s/^port = .*/port = 6432/; }' app.conf | sed -n '/^\[database\]/,/^$/p'[database]
host = db02.internal
port = 6432
name = orders
user = orders_app
max_connections = 50
When a sed program grows past two or three commands, put it in a file and run
sed -f fix-config.sed app.conf; it's easier to read, comment and review. Past that,
it's usually time for awk or a real language.
Editing files in place: -i
-i writes the result back to the input file instead of to stdout:
sed -i 's/^workers = 4/workers = 8/' app.conf
grep -n '^workers' app.conf7:workers = 8No output, no confirmation, no undo. Give -i a suffix and sed keeps the original
under that name first. Then diff shows exactly what changed:
sed -i.bak 's/^max_connections = .*/max_connections = 200/' app.conf
ls app.conf*
diff app.conf.bak app.confapp.conf
app.conf.bak
app.conf.orig
15c15
< max_connections = 50
---
> max_connections = 200(app.conf.orig is a copy I made before starting.) My routine for editing a config with
sed is: run the command without -i and read the output (pipe it to diff against
the original for anything non-trivial), then run it again with -i.bak, then check
diff file.bak file, then delete the backup.
GNU vs BSD/macOS -i
This is the most common portability problem with sed. GNU sed (Linux) takes the
backup suffix attached to -i, and the suffix is optional. BSD sed (macOS,
FreeBSD) requires a suffix argument, so the macOS idiom for "no backup" is
sed -i '' 's/a/b/' file. Run that on Linux and GNU sed takes '' as the script and
your expression as a filename:
sed -i '' 's/^workers = 8/workers = 16/' app.confsed: can't read s/^workers = 8/workers = 16/: No such file or directoryThe form that works on both is an attached suffix, sed -i.bak 's/.../.../' file, then
rm file.bak if you don't want the backup. (I tested the GNU side of this; the macOS
behaviour is from the BSD sed manual.)
-i replaces the file
-i doesn't edit the file where it is: it writes a new temporary file and renames it
over the original. Usually that is invisible. With a symlink it isn't. Here
current.conf is a symlink to app.conf, and ls -li shows inode numbers:
ln -s app.conf current.conf
sed -i 's/^workers = 4/workers = 6/' current.conf
ls -li app.conf current.conf
grep -H '^workers' app.conf current.conf63381 -rw-r--r-- 1 sid sid 369 Nov 9 21:49 app.conf
63393 -rw-r--r-- 1 sid sid 369 Nov 9 21:49 current.conf
app.conf:workers = 4
current.conf:workers = 6The symlink was replaced by an ordinary file with the edit, and the real app.conf was
not changed. Deployments that use a current symlink pointing at a release directory
hit exactly this. GNU sed has --follow-symlinks to edit the target instead:
rm current.conf && ln -s app.conf current.conf
sed -i --follow-symlinks 's/^workers = 4/workers = 6/' current.conf
ls -l current.conf
grep '^workers' app.conflrwxrwxrwx 1 sid sid 8 Nov 9 21:49 current.conf -> app.conf
workers = 6For the same reason, -i gives the file a new inode, which breaks hard links and means
a program holding the old file open keeps reading the old contents.
Real tasks
These are the jobs sed gets used for most, with the commands I'd use.
Change a config value. Anchor to the start of the line, match the key, replace
the whole value, and restrict to the section when the key isn't unique. Shown above:
sed -i.bak '/^\[server\]/,/^$/ s/^port = .*/port = 9090/' app.conf.
Strip comments and blank lines to see what a long config actually sets.
[[:space:]]* also catches indented comments and lines containing only spaces:
sed -e '/^[[:space:]]*#/d' -e '/^[[:space:]]*$/d' app.conf | wc -l16Deleting trailing comments (key = value # note) with s/#.*// is tempting, but it
also truncates any value that contains #, such as passwords and URL fragments, so I
only do it when I know the file.
Extract a section's settings without its header. The range runs from the
[database] header to the next header; the inner block prints only lines that are not
headers and not blank:
sed -n '/^\[database\]/,/^\[/{/^\[/!{/^$/!p}}' app.confhost = db01.internal
port = 5432
name = orders
user = orders_app
max_connections = 50That works, and it is also about the limit of what I'd write in sed. The awk version,
in the awk article, is easier to read.
Print a window of a log by line numbers (sed -n '1200,1300p' big.log) or by
pattern, as in the time-window example above.
Where you'll use this
- Dockerfiles and image builds: a typical
RUNline edits a packaged config, such assed -i 's/^#\?PermitRootLogin.*/PermitRootLogin no/' /etc/ssh/sshd_config. - CI pipelines: bumping a version string before a release build.
- Install and provisioning scripts: filling in templates with per-environment values.
- Log handling: anonymising or redacting before sharing, cutting time windows out of big files.
A version bump in CI looks like this. Double quotes let the shell insert $new; the
inner quotes are escaped so they reach sed:
printf '%s\n' '[project]' 'name = "orders-service"' 'version = "2.3.1"' > pyproject.toml
new=2.4.0
sed -i -E "s/^version = \"[^\"]*\"/version = \"$new\"/" pyproject.toml
cat pyproject.toml[project]
name = "orders-service"
version = "2.4.0"A complete example: rendering a config template
Many deployments ship a template with placeholders and fill it in per environment:
cat orders.conf.tmpl[database]
host = @DB_HOST@
port = @DB_PORT@
password = @DB_PASS@The obvious one-liner, sed "s/@DB_PASS@/$DB_PASS/", works until a value contains a
character that is special in a replacement. Both of these are real runs:
DB_PASS='Tr0ub&dor'
sed "s/@DB_PASS@/$DB_PASS/" orders.conf.tmpl | grep password
DB_PASS='s3cr3t/42'
sed "s/@DB_PASS@/$DB_PASS/" orders.conf.tmpl | grep passwordpassword = Tr0ub@DB_PASS@dor
sed: -e expression #1, char 22: unknown option to `s'The / fails loudly. The & is worse: it was replaced by the matched text, and the
service would start with a wrong password. A safe renderer escapes the values first,
checks its inputs, and refuses to produce a file with placeholders left in it:
#!/usr/bin/env bash
# render-config.sh - fill @PLACEHOLDERS@ in a config template from environment variables.
# usage: DB_HOST=... DB_PORT=... DB_PASS=... render-config.sh TEMPLATE OUTPUT
set -euo pipefail
template=$1
output=$2
# Check every input up front: an unset variable inside $( ) below would
# only kill the subshell, and the script would carry on with an empty value.
: "${DB_HOST:?must be set}" "${DB_PORT:?must be set}" "${DB_PASS:?must be set}"
# Backslash, slash and & are special in a sed replacement; escape them.
sed_escape() { printf '%s' "$1" | sed 's/[\/&]/\\&/g'; }
sed -e "s/@DB_HOST@/$(sed_escape "$DB_HOST")/g" \
-e "s/@DB_PORT@/$(sed_escape "$DB_PORT")/g" \
-e "s/@DB_PASS@/$(sed_escape "$DB_PASS")/g" \
"$template" > "$output.tmp"
# Refuse to install a config that still contains a placeholder.
if grep -n '@[A-Z_]*@' "$output.tmp"; then
echo "render-config: unfilled placeholders in $output" >&2
rm -f "$output.tmp"
exit 1
fi
mv "$output.tmp" "$output"
echo "rendered $output"With a password containing &, / and a backslash:
DB_HOST=db02.internal DB_PORT=6432 DB_PASS='Tr0ub&dor/42\x' ./render-config.sh orders.conf.tmpl orders.conf
cat orders.confrendered orders.conf
[database]
host = db02.internal
port = 6432
password = Tr0ub&dor/42\xWith a variable missing, and with a template that has a placeholder the script doesn't know about:
DB_HOST=db02.internal DB_PASS='x' ./render-config.sh orders.conf.tmpl orders.conf
echo "exit: $?"
printf '%s\n' '[database]' 'host = @DB_HOST@' 'user = @DB_USER@' > users.conf.tmpl
DB_HOST=db02.internal DB_PORT=6432 DB_PASS=x ./render-config.sh users.conf.tmpl users.conf
echo "exit: $?"./render-config.sh: line 11: DB_PORT: must be set
exit: 1
3:user = @DB_USER@
render-config: unfilled placeholders in users.conf
exit: 1The up-front ${VAR:?} line exists because of a test failure. My first version relied on
set -u alone. With DB_PORT unset, it printed DB_PORT: unbound variable, then
rendered orders.conf, exited 0, and wrote port = with no value. The unset variable
was expanded inside $( ), so set -u killed only that subshell; the outer sed
ran anyway with an empty replacement. Checking inputs before any command substitution
closes that gap. (set -euo pipefail is covered in
error handling, traps, and debugging.)
The escaping still assumes values contain no newlines. For anything more complex than placeholders, use a real template tool.
Common mistakes
A slash in the replacement
new_root=/srv/www/shop
echo "root /var/www/shop;" | sed "s/root .*/root $new_root;/"sed: -e expression #1, char 17: unknown option to `s'After the shell expanded $new_root, sed saw s/root .*/root /srv/www/shop;/: the
/ after root ended the replacement and srv... was read as flags. Use a delimiter
that can't appear in the value:
echo "root /var/www/shop;" | sed "s|root .*|root $new_root;|"root /srv/www/shop;-n and -i together
cp app.conf demo.conf
sed -n -i 's/^port = 8080/port = 9090/p' demo.conf
cat demo.confport = 9090The whole file is now one line. -n suppresses the automatic print, -i writes
exactly what was printed back to the file, and only the changed line was printed. Never
combine -n with -i unless you mean "keep only these lines". This is the best
argument for always using -i.bak.
Greedy .*
.* matches as much as possible. Trying to blank out the user agent (the last quoted
field) of a log line:
echo '"GET /cart HTTP/1.1" 200 3921 "https://shop.example.com/" "curl/8.5.0"' | sed 's/".*"/"-"/'"-"The match ran from the first quote on the line to the last one. Match "a quote, then anything except a quote, then a quote" and pick the third occurrence:
echo '"GET /cart HTTP/1.1" 200 3921 "https://shop.example.com/" "curl/8.5.0"' | sed 's/"[^"]*"/"-"/3'"GET /cart HTTP/1.1" 200 3921 "https://shop.example.com/" "-"sed regular expressions have no non-greedy .*?; negated classes are the tool.
! inside double quotes at the prompt
In an interactive shell, ! inside double quotes triggers history expansion. Here I
wanted to print only the comment lines (/^#/!d, "delete lines that are not
comments"):
sed "/^#/!d" app.confsed "/^#/diff app.conf.bak app.conf" app.conf
sed: -e expression #1, char 6: extra characters after commandBash replaced !d with the most recent command starting with d (a diff I had run
earlier), printed the rewritten line and ran it. With no matching command in your
history you get event not found instead. Single quotes don't do history expansion:
sed '/^#/!d' app.conf# app.conf - order service configuration
# Managed by provisioning; local edits may be overwritten.
# tls_cert = /etc/ssl/certs/orders.pemScripts don't do history expansion, so this only bites at the prompt, but it's one more
reason to single-quote sed programs by default.
Summary
sedreads a line into the pattern space, runs the script, prints the result (unless-n), and repeats. It writes to stdout and leaves files alone unless you use-i.s/re/rep/replaces the first match; flagsg,N,Iandpchange that. Use another delimiter (s|a|b|) when slashes are involved, and escape&,/and\in replacements built from variables.- Use
-Efor capture groups:( )captures,\1to\9reuse; negated classes like[^"]*replace non-greedy matching. - Addresses limit commands: line numbers,
$,/re/, rangesa,b, and GNU's~and,+N. A range whose end never matches runs to EOF; one whose start never matches selects nothing. ddeletes,qquits early,=prints line numbers,a/i/cadd or replace lines; combine with-e,;,{ }or-f.- Edit with
-i.bakand check withdiff. GNU and BSD-idiffer;-i.bakworks on both.-ireplaces symlinks and breaks hard links; never mix-nwith-i.
Exercises
Print the value of every host setting in app.conf, and nothing else, with a single
sed command.
Show Solution
sed -n 's/^host = //p' app.conf0.0.0.0
db01.internal
cache01.internal-n suppresses normal output; the p flag prints only lines where the substitution
(deleting host = ) happened.
Change the [server] port to 9090 in app.conf, in place, keeping a backup. The other
three port lines must not change. Prove it with diff.
Show Solution
sed -i.bak '/^\[server\]/,/^$/ s/^port = .*/port = 9090/' app.conf
diff app.conf.bak app.conf
grep -n '^port' app.conf6c6
< port = 8080
---
> port = 9090
6:port = 9090
12:port = 5432
19:port = 6379
23:port = 9100The range /^\[server\]/,/^$/ limits the substitution to the server section, and the
diff shows exactly one changed line.
Rewrite the dates in orders.csv from YYYY-MM-DD to DD/MM/YYYY, printing the result
(don't modify the file). The header must pass through untouched.
Show Solution
sed -E 's/([0-9]{4})-([0-9]{2})-([0-9]{2})/\3\/\2\/\1/' orders.csv | head -n 3order_id,date,region,product,qty,unit_price
1001,01/11/2026,north,keyboard,2,49.99
1002,01/11/2026,south,monitor,1,189.00The header has no date, so the pattern simply doesn't match it. Using | as the
delimiter (s|...|\3/\2/\1|) would avoid escaping the slashes in the replacement.
Make a copy of access.log called access-anon.log. In place, with a backup, (a) zero
the last octet of every client IP and (b) remove query strings from request paths
(/products?page=2 becomes /products). Leave the referrer field alone. Verify the
result.
Hint
For (b), match from the start of the line up to the request path with
^([^"]*"[A-Z]+ [^ ?]*), then a literal ? and everything up to the next space.
Show Solution
cp access.log access-anon.log
sed -i.bak -E -e 's/^([0-9]+\.[0-9]+\.[0-9]+)\.[0-9]+ /\1.0 /' -e 's/^([^"]*"[A-Z]+ [^ ?]*)\?[^ ]*/\1/' access-anon.log
grep 'products' access-anon.log | cut -d' ' -f1,7198.51.100.0 /products/42
198.51.100.0 /img/products/42.jpg
203.0.113.0 /products
203.0.113.0 /products/42
203.0.113.0 /cart
198.51.100.0 /products/42grep -n '?' access-anon.log | cut -c1-12021:203.0.113.0 - - [09/Nov/2026:09:03:37 +0000] "GET /cart HTTP/1.1" 200 3921 "https://shop.example.com/products?page=2"The only ? left is in a referrer, which the anchored pattern deliberately can't reach:
[^"]*" stops at the first quote, so the match is always the request field. In ERE,
\? is a literal question mark. diff access-anon.log.bak access-anon.log shows every
line changed, because every IP was rewritten.
Tested with: GNU sed 4.9 and GNU Bash 5.2.21 on Ubuntu 24.04 (WSL2), November 2026.
Every command was run as shown, mostly in an interactive shell so the history-expansion
example behaves as it does at a prompt. BSD/macOS sed was not available, so the notes
on sed -i '', \U and the one-line a text form describe its documented behaviour
rather than tested output. The PermitRootLogin expression in "Where you'll use this"
was tested on a two-line sample file, not inside a real image build.
Written by The Dark Debugger
Engineer documenting the things that took me too long to learn, so they take you less time. Every Bash tutorial here is tested end to end before publishing.