Sed for replace a substring inside a string with a pattern

I am trying to replace a part of a pattern, such as, if I have col(3,B,14), after applying the sed command, I would like to get col(3,B,t14) which adds the t character to the third parameter in the pattern.

I am trying with :

s="col(3,B,14)"
echo $s | sed 's/col([0-9],[A-Z],[0-9])/col([0-9],[A-Z],t[0-9])/g'

But, it returns the original string. I would appreciate it if you could give some advice. Thanks.

2

1 Answer

You seem a bit confused about how sed works, so I'll go step by step. My "answer" is this:

s="col(3,B,14)"; echo $s | sed 's/\(col([0-9],[A-Z],\)/\1t/g'

Explanation

There are a couple of problems here.

  • First, you need a semicolon (;) after defining your variable s, before echoing it.

     s="col(3,B,14)"; echo $s 
  • Next, sed substitution works by s/pattern/replacement/, where pattern is a regular expression, but where replacement is not. That is, putting something like [0-9] in the replacement will not represent any digit, but will instead represent the five characters: [, 0, -, 9, and ].

  • Also, the /g at the end is used to keep applying the substitution on a string for every match of the pattern, so if you had a line like:

    echo hello world | sed 's/o/z/g'

    then the output would be:

    hellz wzrld

    whereas:

    echo hello world | sed 's/o/z/'

    would give:

    hellz world
  • Let's remove your replacement for now:

    s="col(3,B,14)"; echo $s | sed 's/col([0-9],[A-Z],[0-9])/replacement/g'

    Turning attention to the regular expression pattern you used, it says "match a string like col(\<single digit>,\<uppercase letter>,\<single digit>)". Notice that the last [0-9] piece won't match 14, since 14 is a two-digits number and so your pattern would match col(3,B,1), but will not match col(3,B14). To match one or more digits, you can use [0-9][0-9]*.

  • To do the replacement as you want, the best way would be to use a capture group. Capture groups "remember" part of the match for later use. You put \( and \) around the part of the pattern you want to remember and use \1 to refer to it later:

    s="col(3,B,14)"; echo $s | sed 's/\(col([0-9],[A-Z],\)/\1replacement/g'

    This will match col(\<single digit>,\<uppercase letter>,, so up to and including the point where you want to add a t. All of this matched stuff will be put back in the replacement (\1) followed by any text you add (in this case we're adding the literal text "replacement"). Any remaining text not matched in the input will be unaffected. The above will output:

    col(3,B,replacement14)
  • So if we now put a "t" in the replacement string:

    s="col(3,B,14)"; echo $s | sed 's/\(col([0-9],[A-Z],\)/\1t/g'

    we get:

    col(3,B,t14)

If you want to learn sed well, I can recommend an excellent tutorial:

4

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like