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
command linetext processing
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
Best Answer
Using
sed
: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 leading2:
substring (e.g. there is not a chance of a1:202:25
string, which the following shortened command would replace to1:202:0
), the command might be shortened to this:Command #1 / #2 breakdown:
-E
: makessed
intepret the pattern as an ERE (Extended Regular Expression) pattern;> out
: redirectsstdout
toout
;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 strings2
: matches a2
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 group0
: adds a0
character/
: stops the replacement string / starts the pattern flagsg
: asserts to perform the substitution globally, i.e. to substitute each occurence of the pattern in the linesed
command #2 breakdown:s
: asserts to performs a substitution/
: starts the pattern2
: matches a2
character:
: matches a:
character[^ ]*
: matches any number of characters not/
: stops the pattern / starts the replacement string2:0
: adds a2:0
string/
: stops the replacement string / starts the pattern flagsg
: asserts to perform the substitution globally, i.e. to substitute each occurence of the pattern in the line