Issue
I have a string in my file
quipkgkulul,qyyio (avuip),Llo,tyiop : D[0x000]: 0x00123400 0x00000000 0x00000000 0x00000000
i want to locate D[0x000]
and replace positions in the digits following the colon (all of them) at different positions.
the final result should look like
quipkgkulul,qyyio (avuip),Llo,tyiop : D[0x000]: 0x00xxx00 0x000xxx0 0x00000000 0x00000000
i tried
sed -E 's/[^[][a-zA-Z0-9][^]]//g' file
which separates out the numbers from the brackets
Solution
If you really want what is described in example, you can use this command:
sed -E 's/(D\[0x[[:digit:]]{3}\]:[[:space:]]+0x[[:digit:]]{2})[[:digit:]]{4}([[:digit:]]{2}[[:space:]]+0x[[:digit:]]{3})[[:digit:]]{4}([[:digit:]]{1})/\1xxx\2xxx\3/g' file
It will find every block of the following format:
D[0x000]: 0x00123400 0x00000000
^^^ ^^^^^^^^ ^^^^^^^^
|||| ||||
where ^
marks any digit, |
symbols that will be replaced with xxx
.
Explanation of matching pattern:
(D\[0x[[:digit:]]{3}\]:[[:space:]]+0x[[:digit:]]{2})
matches block before first replaced segment. Later referenced in replacement string as\1
. Here:D\[0x[[:digit:]]{3}\]:
matches strings likeD[0x000]:
where last three zeros can be any digits,[[:space:]]+
matches any positive umber of whitespace symbols,0x[[:digit:]]{2}
matches first part of first hex address, like0x00
, where last two zeros can be any digit.
[[:digit:]]{4}
any four digits that later will be replaced withxxx
,([[:digit:]]{2}[[:space:]]+0x[[:digit:]]{3})
matches second group (\2
). Two digits, spaces,0x
, three digits.[[:digit:]]{4}
second replaced segment,([[:digit:]]{1})
last digit, referenced as\3
.
Answered By - markalex Answer Checked By - Marie Seifert (WPSolving Admin)