1

I have abc.txt file that has below entries. I would like to replace substring of the sentence that begins with "test" after the second colon (:) with given value.

Tried below:

echo test:abc.io/gng-cp:1.34.2-of1231 | sed 's/\([^test:]*:\).*/\11.333.555/g'

But it is giving:

test:1.333.555

Expected was:

test:abc.io/gng-cp:1.333.555

Input is:

abc.txt
=======
jnu
mango
vim
test:abc.io/gng-cp:1.34.2-of1231

Appreciate inputs on how to fix this?

0

2 Answers 2

1

The expression [^test:]* means: match zero or more of characters that are NOT in the set {t, e, s, t, :}, which is probably not what you want. You're passing the global /g modifier to the expression, even though you're only modifying the string once. Also, it's not clear where you're capturing the abc.io/gng-cp portion of the string to preserve it.

Try this:

sed 's/^\(test:[^:]*:\).*$/\11.333.555/' abc.txt

This will match a string starting with test: and preserve it and everything else between the two colons, replacing everything after the second colon with the new number string.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much it working .. appreciate the help @mwp
0

Following awk may also help you.

echo "test:abc.io/gng-cp:1.34.2-of1231" |  awk -v val="1.333.555" 'match($0,/:[^:]*/){print substr($0,1,RSTART+RLENGTH) val;next} 1' Input_file

Adding a non-one liner form of solution now.

echo "test:abc.io/gng-cp:1.34.2-of1231" |  awk -v val="1.333.555" '
match($0,/:[^:]*/){
  print substr($0,1,RSTART+RLENGTH) val;
  next}
1'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.