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

user438383

I would like to take the first 2 lines from test.txt:

hello
my 
name
is

and put them at the top of a new file, output.txt:

foo
bar 

with the desired output being, output.txt:

hello 
my
foo
bar

using sed. However, using:

text=$(head test.txt -n 2) | sed -i "1i $text" output.txt

returns the error:

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

What am I doing wrong?

oliv

First you don't have to use pipe between the variable assignment text= and the sed command. You need to replace with the command delimiter ;.

Second, you can get the error because part of the replacement text is interpreted by sed. You actually need to quote the replacement string. This can be done with the bash parameter expansion operator @Q.

text=$(head -n2 test.txt)
sed -i "1i ${text@Q}" output.txt

As mentioned in the bash man pages:

${parameter@operator}
...
Q     The expansion is a string that is the value of parameter quoted in a format that can be reused as input.

Obviously inserting before line 1 is the same as doing:

head -n2 text.txt > /tmp/temp.txt
cat output.txt >> /tmp/temp.txt && mv /tmp/temp.txt ouput.txt

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

sed: -e expression #1, char 10: missing command

From Dev

sed: -e expression #1, char 1: unknown command: `''

From Dev

sed: -e expression #1, char 1: unknown command: '|'

From Dev

sed: -e expression #1, char 4: unknown command:

From Dev

bash - sed: -e expression #1, char 15: unterminated `s' command

From Dev

sed: -e expression #1, char 35: unterminated `s' command

From Dev

sed: -e expression #1, char 37: unterminated `s' command

From Dev

sed: -e expression #1, char XX: unterminated `s' command

From Dev

sed: -e expression #1, char 5: unterminated `s' command

From Dev

sed error complaining "extra characters after command"

From Dev

sed on mac: 'extra characters after p command'

From Dev

sed Error sed: -e expression #1, char 7: unterminated `s' command

From Dev

sed: -e expression #1, char 3: unexpected `,'

From Dev

`sed: -e expression #1, char 4: unknown command:` but the line endings are fine

From Dev

bash sed: -e expression #1, char 21: number option to `s' command may not be zero

From Dev

sed: -e expression #1, char 23: unknown option to `s'

From Dev

sed: -e expression #1, char 44: unterminated address regex

From Dev

/bin/sed: -e expression #1, char 56: unknown option to `s'

From Java

How to fix sed command on MacOS with error extra characters after \ at the end of c command?

From Dev

why "extra characters after command" error shown for the sed command line shown?

From Dev

sed extra characters at end of l command

From Dev

BSD sed: extra characters at the end of d command

From Dev

sed throwing sed: -e expression #(number), char (number): unknown command: `(letter)'

From Dev

sed throwing sed: -e expression #(number), char (number): unknown command: `(letter)'

From Dev

Sed - How do I remove the extra characters that show up after running the command?

From Dev

Why does sed output -e expression #1, char 55: Invalid preceding regular expression

From Dev

sed: -e expression #3, char 59: unknown option to `s'

From Dev

Sed matches unwanted extra characters

From Dev

sed command with regular expression

Related Related

  1. 1

    sed: -e expression #1, char 10: missing command

  2. 2

    sed: -e expression #1, char 1: unknown command: `''

  3. 3

    sed: -e expression #1, char 1: unknown command: '|'

  4. 4

    sed: -e expression #1, char 4: unknown command:

  5. 5

    bash - sed: -e expression #1, char 15: unterminated `s' command

  6. 6

    sed: -e expression #1, char 35: unterminated `s' command

  7. 7

    sed: -e expression #1, char 37: unterminated `s' command

  8. 8

    sed: -e expression #1, char XX: unterminated `s' command

  9. 9

    sed: -e expression #1, char 5: unterminated `s' command

  10. 10

    sed error complaining "extra characters after command"

  11. 11

    sed on mac: 'extra characters after p command'

  12. 12

    sed Error sed: -e expression #1, char 7: unterminated `s' command

  13. 13

    sed: -e expression #1, char 3: unexpected `,'

  14. 14

    `sed: -e expression #1, char 4: unknown command:` but the line endings are fine

  15. 15

    bash sed: -e expression #1, char 21: number option to `s' command may not be zero

  16. 16

    sed: -e expression #1, char 23: unknown option to `s'

  17. 17

    sed: -e expression #1, char 44: unterminated address regex

  18. 18

    /bin/sed: -e expression #1, char 56: unknown option to `s'

  19. 19

    How to fix sed command on MacOS with error extra characters after \ at the end of c command?

  20. 20

    why "extra characters after command" error shown for the sed command line shown?

  21. 21

    sed extra characters at end of l command

  22. 22

    BSD sed: extra characters at the end of d command

  23. 23

    sed throwing sed: -e expression #(number), char (number): unknown command: `(letter)'

  24. 24

    sed throwing sed: -e expression #(number), char (number): unknown command: `(letter)'

  25. 25

    Sed - How do I remove the extra characters that show up after running the command?

  26. 26

    Why does sed output -e expression #1, char 55: Invalid preceding regular expression

  27. 27

    sed: -e expression #3, char 59: unknown option to `s'

  28. 28

    Sed matches unwanted extra characters

  29. 29

    sed command with regular expression

HotTag

Archive