Skip to content

/errors/sed/extra-characters-after-command

sed: -e expression #1, char N: extra characters after command

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · sed (GNU sed) 4.9updated
tool
sed
exit code
1
versions
GNU sed 4.x
os
linux, windows

sed: -e expression #1, char 5: extra characters after command means GNU sed finished reading a command and found more text where it expected a separator. Most often two commands are on one line without a ;, like sed -n '1p 2p'. Write sed -n '1p;2p', or pass each command with its own -e. char N is the position where parsing stopped, and sed exits with status 1 without reading any input.

Reproduce the error

bash
printf 'one\ntwo\nthree\n' > list.txt
sed -n '1p 2p' list.txt

Output · exit 1

sed: -e expression #1, char 4: extra characters after command

A command that takes no argument (d, p, =, n) followed by more text fails the same way:

bash
printf 'one\ntwo\nthree\n' > list.txt
sed '/two/d d' list.txt

Output · exit 1

sed: -e expression #1, char 8: extra characters after command

Fix: separate the commands

bash
printf 'one\ntwo\nthree\n' > list.txt
sed -n '1p;3p' list.txt
sed -n -e '1p' -e '3p' list.txt

Output

one
three
one
three

Inside braces, GNU sed accepts the closing } after a ;:

bash
printf 'one\ntwo\nthree\n' > list.txt
sed -n '/t/{s/t/T/;p;}' list.txt

Output

Two
Three

Which commands end where

CommandWhat may follow it on the same line
d, D, p, P, n, N, g, G, h, H, x, =, l, zonly ;, } or a newline
q, Qan optional exit code, then ; or newline
s/a/b/FLAGS, y/a/b/flags (for s), then ; or newline
a, i, c TEXT (GNU one-line form)everything up to the newline is the text
r, w FILEeverything up to the newline is the file name
b, t LABELGNU allows ; after the label

Because a, i, c, r and w take the rest of the line, a ; after them becomes part of the text or the file name, not a separator. Put them last on the line or on a line of their own.

Sources