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

Add text to text in file with bash

I’m not a bash expert so that is why I’m asking for help to manipulate strings in file. I know about sed but I’m having hard time trying.

Basically I have a file with the following content:

docs: lala

docs: this is the description

That is the footer

I need to add to the first line only docs: lala the following text: ($SCOPE) which should look like this in the end:

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

docs(some_scope_name): lala

docs: this is the description

That is the footer

What I have is this:

sed -i $FILE -e "s/docs:/docs:($SCOPE_NAME)/"

This works just fine, BUT is replacing also the second docs. I only need the first match!

Also not only docs should match, but also other types, those are:

build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test

For example, it could also be:

feat(some_scope_name): lala

feat: this is the description

That is the footer

How should I improve my sed to achieve that?

EDIT

The suggested answer by @anubhava

sed -E "0,/^[[:blank:]]*(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test):/ s//\1(lala):/" $FILE

works fine but it’s still replacing the sencond match:

docs(lala): lala

docs(lala): this is the description

That is the footer

>Solution :

You may use this gnu-sed command:

sed -i -E "1s/^[[:blank:]]*(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test):/\1($SCOPE_NAME):/" file
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