DAG
DAG

Reputation: 2620

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:

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

Upvotes: 1

Views: 39

Answers (2)

anubhava
anubhava

Reputation: 784898

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

Upvotes: 1

sseLtaH
sseLtaH

Reputation: 11207

Using sed

$ scope=some_scope_name
$ sed "1s/\([^:]*\)\(.*\)/\1($scope)\2/" input_file
docs(some_scope_name): lala

docs: this is the description

That is the footer

Upvotes: 0

Related Questions