Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

bash insert word at first position in file using sed

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading