Quicksilver
Quicksilver

Reputation: 1

Replace words but only after a colon

I have been researching this for quite some time but cannot seem to find an answer. Perhaps someone here can help.

I am trying to use sed to replace words in yml / yaml files. Since some of the words are included in the names I want to only replace words that appear after the colon (':').

For example. If the .yml file includes:

en:
  label_some_tracker: A tracker
  label_all_tracker: All trackers
  label_attachment_type_trackers: Select trackers.
  tracker_plural: trackers

and I want to replace all occurrences of tracker with issue in all values. The pattern:

s/tracker/issue/

also changes the names of the fields, which breaks my code.

I can reduce the size of the problem somewhat by including terms for all possible variants of a word. For example:

s/trackers/issues/
s/tracker/issue/

but that doesn't deal with all situations.

I have tried inserting a space before the search term:

s/ tracker/ issue/

but that matches names where the search term is at the beginning of the line.

If I search for whole words then it still seems to pick up the names because ':' and '_' are 'non word' characters.

If I try to put spaces at the beginning and end of the search term but then it misses words that are at the end of a line or words patterns with punctuation marks before the training space.

The only sure way seems to be to only replace words after a colon (':') but I cannot seem to figure out how to do that with sed.

Does anyone here know how?

Upvotes: 0

Views: 214

Answers (2)

Walter A
Walter A

Reputation: 20032

Replace second occurance:

 sed 's/tracker/issue/2' file

Upvotes: 0

Cyrus
Cyrus

Reputation: 89009

With GNU sed:

sed -E 's/(:.*)tracker/\1issue/g' file

Output:

en:
  label_some_tracker: A issue
  label_all_tracker: All issues
  label_attachment_type_trackers: Select issues.
  tracker_plural: issues

Upvotes: 2

Related Questions