bash insert word at first position in file using sed

Viewed 32

How can I use sed to insert a word at the first line of a file at start position. For instance I have the file witn the following content:

that is a test without scope
that is the second line
that is the third line

The result should be:

[TICKET_NUMBER] that is a test without scope
that is the second line
that is the third line

I'm trying this, but I get the following error:

sed -i -E "1s/^[$ISSUE_ID]/ " $COMMIT_EDITMSG_FILE

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

I've followed some examples, but I can not make any progress.

1 Answers

You may use this sed:

sed "1s/^/$ISSUE_ID /" file

[TICKET_NUMBER] that is a test without scope
that is the second line
that is the third line

Here:

  • 1s will apply s command on first line only
  • ^ will match line start
  • "..." will let shell variable expand
Related