Solved - Sed - Positional Replace

Hi All,

I don't know what I am doing wrong in the regex below.

I have the following string:
31000000010201005181121360000000100000000003000000YYY-YYY-YYY-20100518-104139.txt.YYY

I need to split it in parts:

  • Group 1: 3100000001020100518112136
  • Group 2: 000000010
  • Group 3: 0000000003000000
  • Group 4: YYY-YYY-YYY-20100518-104139.txt.YYY

Check below my sed regex execution:

$ a="31000000010201005181121360000000100000000003000000YYY-YYY-YYY-20100518-104139.txt.YYY"

$ echo "${a}" | sed 's/\(31[0-9]\{23\}\)\([0-9]\{9\}\)\([0-9]\{16\}\)\(.*$\)/\2xxx\3/g'

# The result:
31000000010201005181121360000000100000000003000000YYY-YYY-YYY-20100518-104139.txt.YYY

Why it prints the whole string instead of printing only the groups I asked? What I am doing wrong?

Thanks a lot!

---------- Post updated at 04:21 PM ---------- Previous update was at 03:51 PM ----------

Ok! I am going to answer my own question! Hahah... Sorry!

After doing some tests and splitting my regex in parts I found that the problem was in the $ in the last group: (.*$).

You can use the following regex, it worked!

echo "${a}" | sed 's/^\(31[0-9]\{23\}\)\([0-9]\{9\}\)\([0-9]\{16\}\)\(.*\)/\1XX\2XX\3XX\4/g'

Thanks!