How to replace all strings in a file that begin with some prefix

Example:

1:20 2:25 3:0.432 2:-17 10:12

I want to replace all strings that begin with 2: to 2:0.

Output:

1:20 2:0 3:0.432 2:0 10:12

Using sed:

sed -E 's/((^| )2:)[^ ]*/\10/g' in > out

Also, as inspired by souravc's answer, if there is not a chance of a 2: substring after the start of a string not containing a leading 2: substring (e.g. there is not a chance of a 1:202:25 string, which the following shortened command would replace to 1:202:0), the command might be shortened to this:

sed -E 's/2:[^ ]*/2:0/g' in > out

Command #1 / #2 breakdown:

  • -E: makes sed intepret the pattern as an ERE (Extended Regular Expression) pattern;
  • > out: redirects stdout to out;

sed command #1 breakdown:

  • s: asserts to performs a substitution
  • /: starts the pattern
  • (: starts the capturing group
  • (: starts grouping the allowed strings
  • ^: matches the start of the line
  • |: separates the second allowed string
  • : matches a character
  • ): stops grouping the allowed strings
  • 2: matches a 2 character
  • :: matches a : character
  • ): stops the capturing group
  • [^ ]*: matches any number of characters not
  • /: stops the pattern / starts the replacement string
  • \1: backreference replaced with the first capturing group
  • 0: adds a 0 character
  • /: stops the replacement string / starts the pattern flags
  • g: asserts to perform the substitution globally, i.e. to substitute each occurence of the pattern in the line

sed command #2 breakdown:

  • s: asserts to performs a substitution
  • /: starts the pattern
  • 2: matches a 2 character
  • :: matches a : character
  • [^ ]*: matches any number of characters not
  • /: stops the pattern / starts the replacement string
  • 2:0: adds a 2:0 string
  • /: stops the replacement string / starts the pattern flags
  • g: asserts to perform the substitution globally, i.e. to substitute each occurence of the pattern in the line

This one liner using sed

sed -i.bkp 's/2:\([0-9]*\)\|2:\(-\)\([0-9]*\)/2:0/g' input_file

will in line replace globally in input_file keep a backup file named input_file.bkp at the same directory.

This can be further shorten using extended regexes as suggested by kos, as

sed -ri.bkp 's/2:\-?[0-9]*/2:0/g' input_file