/errors/sed/extra-characters-after-command
sed: -e expression #1, char N: extra characters after command
- 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
printf 'one\ntwo\nthree\n' > list.txt
sed -n '1p 2p' list.txtOutput · 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:
printf 'one\ntwo\nthree\n' > list.txt
sed '/two/d d' list.txtOutput · exit 1
sed: -e expression #1, char 8: extra characters after command
Fix: separate the commands
printf 'one\ntwo\nthree\n' > list.txt
sed -n '1p;3p' list.txt
sed -n -e '1p' -e '3p' list.txtOutput
one three one three
Inside braces, GNU sed accepts the closing } after a ;:
printf 'one\ntwo\nthree\n' > list.txt
sed -n '/t/{s/t/T/;p;}' list.txtOutput
Two Three
Which commands end where
| Command | What may follow it on the same line |
|---|---|
| d, D, p, P, n, N, g, G, h, H, x, =, l, z | only ;, } or a newline |
| q, Q | an 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 FILE | everything up to the newline is the file name |
| b, t LABEL | GNU 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.