0

I have a string in a file:

0x0c42a103001815f6, MF0;r-hpc-sw07:MQM8700/U1 , Valid , 2022 Mar 29 11:35:54:600401

0x0c42a10300181636, MF0;r-hpc-sw09:MQM8700/U1 , Valid , 2022 Mar 29 11:35:54:600447

0x98039b03008d5d44, MF0;r-hpc-sw08:MQM8700/U1 , Valid , 2022 Mar 29 11:35:54:600476

0x98039b0300a8b786, MF0;r-hpc-sw10:MQM8700/U1 , Valid , 2022 Mar 29 11:35:54:600529

======================================

0x0c42a103001815ee, 1, 0x0c42a1030018162e, 1, 0

0x0c42a1030018162e, 8, 0x98039b03008d5d3c, 8, 0

0x0c42a103001815ee, 8, 0x98039b0300a8b77e, 8, 0

0x98039b03008d5d3c, 1, 0x98039b0300a8b77e, 1, 0

I would like to replace in the second paragraph only the last number '0' to '9':

0x0c42a103001815f6, MF0;r-hpc-sw07:MQM8700/U1 , Valid , 2022 Mar 29 11:35:54:600401

0x0c42a10300181636, MF0;r-hpc-sw09:MQM8700/U1 , Valid , 2022 Mar 29 11:35:54:600447

0x98039b03008d5d44, MF0;r-hpc-sw08:MQM8700/U1 , Valid , 2022 Mar 29 11:35:54:600476

0x98039b0300a8b786, MF0;r-hpc-sw10:MQM8700/U1 , Valid , 2022 Mar 29 11:35:54:600529

======================================

0x0c42a103001815ee, 1, 0x0c42a1030018162e, 1, 9

0x0c42a1030018162e, 8, 0x98039b03008d5d3c, 8, 9

0x0c42a103001815ee, 8, 0x98039b0300a8b77e, 8, 9

0x98039b03008d5d3c, 1, 0x98039b0300a8b77e, 1, 9

I tried using: sed -i -r 's|(^([[:alnum:]]+)([[:punct:]]+)([[:space:]]+)([[:digit:]]+)([[:punct:]]+)([[:space:]]+)([[:alnum:]]+)([[:punct:]]+)([[:space:]]+)([[:digit:]]+)([[:punct:]]+)([[:space:]]+)(0)$)|\19|g'

But got:

0x0c42a103001815f6, MF0;r-hpc-sw07:MQM8700/U1 , Valid , 2022 Mar 29 11:35:54:600401

0x0c42a10300181636, MF0;r-hpc-sw09:MQM8700/U1 , Valid , 2022 Mar 29 11:35:54:600447

0x98039b03008d5d44, MF0;r-hpc-sw08:MQM8700/U1 , Valid , 2022 Mar 29 11:35:54:600476

0x98039b0300a8b786, MF0;r-hpc-sw10:MQM8700/U1 , Valid , 2022 Mar 29 11:35:54:600529

======================================

0x0c42a103001815ee, 1, 0x0c42a1030018162e, 1, 09

0x0c42a1030018162e, 8, 0x98039b03008d5d3c, 8, 09

0x0c42a103001815ee, 8, 0x98039b0300a8b77e, 8, 09

0x98039b03008d5d3c, 1, 0x98039b0300a8b77e, 1, 09

Any suggestions?

1

1 Answer 1

1

You get the 09 as a result because you capture the whole line and append 9 to it when replacing with \19.

You may move the capturing group #1 end boundary to the location before 0:

sed -i -r 's|^([[:alnum:]]+[[:punct:]]+[[:space:]]+[[:digit:]]+[[:punct:]]+[[:space:]]+[[:alnum:]]+[[:punct:]]+[[:space:]]+[[:digit:]]+[[:punct:]]+[[:space:]]+)(0)$|\19|g' file

Or, since the punctuation in your string is actually just a comma you can simplify this to

sed -i -r 's|^([[:alnum:]]+,[[:space:]]+[0-9]+,[[:space:]]+[[:alnum:]]+,[[:space:]]+[0-9]+,[[:space:]]+)0$|\19|g' file

Note you only need to capture what you need to keep, and you can wrap pattern sequences with a single capturing group. If you do not need the separate values between commas, etc., you need not capture their individual patterns.

See the online demo:

#!/bin/bash
s='0x0c42a103001815f6, MF0;r-hpc-sw07:MQM8700/U1 , Valid    , 2022 Mar 29 11:35:54:600401
0x0c42a10300181636, MF0;r-hpc-sw09:MQM8700/U1 , Valid    , 2022 Mar 29 11:35:54:600447
0x98039b03008d5d44, MF0;r-hpc-sw08:MQM8700/U1 , Valid    , 2022 Mar 29 11:35:54:600476
0x98039b0300a8b786, MF0;r-hpc-sw10:MQM8700/U1 , Valid    , 2022 Mar 29 11:35:54:600529
======================================
0x0c42a103001815ee, 1, 0x0c42a1030018162e, 1, 0
0x0c42a1030018162e, 8, 0x98039b03008d5d3c, 8, 0
0x0c42a103001815ee, 8, 0x98039b0300a8b77e, 8, 0
0x98039b03008d5d3c, 1, 0x98039b0300a8b77e, 1, 0'
sed -E 's|^([[:alnum:]]+,[[:space:]]+[0-9]+,[[:space:]]+[[:alnum:]]+,[[:space:]]+[0-9]+,[[:space:]]+)0$|\19|g' <<< "$s"

Output:

0x0c42a103001815f6, MF0;r-hpc-sw07:MQM8700/U1 , Valid    , 2022 Mar 29 11:35:54:600401
0x0c42a10300181636, MF0;r-hpc-sw09:MQM8700/U1 , Valid    , 2022 Mar 29 11:35:54:600447
0x98039b03008d5d44, MF0;r-hpc-sw08:MQM8700/U1 , Valid    , 2022 Mar 29 11:35:54:600476
0x98039b0300a8b786, MF0;r-hpc-sw10:MQM8700/U1 , Valid    , 2022 Mar 29 11:35:54:600529
======================================
0x0c42a103001815ee, 1, 0x0c42a1030018162e, 1, 9
0x0c42a1030018162e, 8, 0x98039b03008d5d3c, 8, 9
0x0c42a103001815ee, 8, 0x98039b0300a8b77e, 8, 9
0x98039b03008d5d3c, 1, 0x98039b0300a8b77e, 1, 9
Sign up to request clarification or add additional context in comments.

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.