2

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
    You should use back references in the replacement string: gnu.org/software/sed/manual/html_node/…
    – FedKad
    Jun 6, 2021 at 8:19
  • Thank you FedonKadifeli, yes I follow you advice and I found the solution : echo $s | sed -E 's/col((.*),(.*),(.*))/col\1,\2,t\3)/g' using the back references as you suggest. Thank very much.
    – CGeorgi
    Jun 6, 2021 at 8:33

1 Answer 1

6

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-digit number and so your pattern would match col(3,B,1), but will not match col(3,B,14). 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: https://www.grymoire.com/Unix/Sed.html

4
  • Many thanks mattb for the detailed explanation. I will read the sed tutorial, for sure.
    – CGeorgi
    Jun 6, 2021 at 9:05
  • The mattb answer is really great, but there's one mistake. The output after: ``` s="col(3,B,14)"; echo $s | sed 's/(col([0-9],[A-Z],)/\1replacement/g' ``` shoudn't be: ``` col(3,B,1replacement4) ``` but: ``` col(3,B,replacement14) ```
    – vaktare95
    May 20, 2022 at 13:26
  • 1
    Instead of [0-9][0-9]* you could also use [0-9]+ to match numbers with one digit or more. + means "match one or more of the preceding token", whereas * means "match zero or more of the preceding token". May 20, 2022 at 19:30
  • @vaktare95 Fixed! thank you!
    – mattb
    May 26, 2022 at 23:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.