regex: add specific lines if detect more than one hexadecimal digit
-
Hi there,
I have the multi-lines shown below and would like to copy the 3 lines Portindex, Portname & PortState from above if there is more than one hexadecimal digit separated with a colon char.
No changes if there is only one hexadecimal digit or no hexadecimal digit. Thanks in advance.From this:
portIndex: 130
portName: slot1 port18
portState: 1Online
10:00:00:90:fe:c0:60:91portIndex: 131
portName: slot1 port19
portState: 1Online
10:00:00:10:3e:fa:65:88
10:00:00:10:3f:fb:66:99
21:00:00:1a:1b:b5:9b:80portIndex: 132
portName: slot1 port20
portState: 1Online
10:00:00:10:4a:bb:d9:6e
10:00:00:10:4a:ba:52:b9portIndex: 243
portName: slot12 port19
portState: 2OfflineTo this:
portIndex: 130
portName: slot1 port18
portState: 1Online
10:00:00:90:fe:c0:60:91portIndex: 131
portName: slot1 port19
portState: 1Online
10:00:00:10:3e:fa:65:88
portIndex: 131
portName: slot1 port19
portState: 1Online
10:00:00:10:3f:fb:66:99
portIndex: 131
portName: slot1 port19
portState: 1Online
21:00:00:1a:1b:b5:9b:80portIndex: 132
portName: slot1 port20
portState: 1Online
10:00:00:10:4a:bb:d9:6e
portIndex: 132
portName: slot1 port20
portState: 1Online
10:00:00:10:4a:ba:52:b9portIndex: 243
portName: slot12 port19
portState: 2Offline -
@lightning-speed said in regex: add specific lines if detect more than one hexadecimal digit:
if there is more than one hexadecimal digit separated with a colon char.
That statement in ambiguous since all records you have shown have more than 1 hexadecimal digit. That is if you regard the meaning of “digit”. Digit means 1 character of a numeric type.
I suspect what you really meant was more than 1 consecutive line containing hexadecimal numbers of the following format ##:##:##:##:##:##:##:##.
Please read the post pinned to the start called “Please Read this before posting”. Then respect the need to properly provide examples, both before and after in the correct manner.
All too often questions are posed (as you have done) which are ambiguous or in some cases just dummy data. So when someone spends time trying to help only to find the question raiser has failed to properly show information, it leaves the helper wondering why they even bothered.
Terry
-
@Terry-R
Thanks for your advice. What I meant was no changes if there is only one or no hexadecimal shown below. I should have omitted the word digit. Apologies if I have caused any misinterpretation or misunderstanding.Best Regards
-
That still might not be sufficient information for us to help you.
The pinned post that @Terry-R mentioned will direct you to pages on how to ask good questions and how to format posts so that the data comes through correctly. If you refuse to follow that advice, then the best you will get is our guesses as to what you want, which will often result in way too many back-and-forths between you and the people who are willing to make those guesses.
----
Useful References
-
Hello, @lightning-speed, @terry-r and All
Here is a possible solution which will need two consecutive regex S/R !
- First add a few line-breaks at the very end of your file !
Then, from this INPUT text :
portIndex: 130 portName: slot1 port18 portState: 1Online 10:00:00:90:fe:c0:60:91 portIndex: 131 portName: slot1 port19 portState: 1Online 10:00:00:10:3e:fa:65:88 10:00:00:10:3f:fb:66:99 21:00:00:1a:1b:b5:9b:80 portIndex: 132 portName: slot1 port20 portState: 1Online 10:00:00:10:4a:bb:d9:6e 10:00:00:10:4a:ba:52:b9 portIndex: 243 portName: slot12 port19 portState: 2Offline
If we use this first regex S/R :
SEARCH
(?-s)^((?:.+\R){3})((?:(?:^[[:xdigit:]:]+)\R)+)|^((?:.+\R){3})\R
REPLACE
?1\2\1:No\r\n\3
you’ll get this temporary OUTPUT text :
10:00:00:90:fe:c0:60:91 portIndex: 130 portName: slot1 port18 portState: 1Online 10:00:00:10:3e:fa:65:88 10:00:00:10:3f:fb:66:99 21:00:00:1a:1b:b5:9b:80 portIndex: 131 portName: slot1 port19 portState: 1Online 10:00:00:10:4a:bb:d9:6e 10:00:00:10:4a:ba:52:b9 portIndex: 132 portName: slot1 port20 portState: 1Online No portIndex: 243 portName: slot12 port19 portState: 2Offline
=> This first regex
-
Write the lines containing hexadecimal characters first, followed with the
3
lines of code of the current section -
Inserts a
No
line, followed with the3
lines of code, if the current section does not contain any line with hexa chars
Then using this second regex S/R :
SEARCH
(?:((?:^[[:xdigit:]:]+)\R)|^No\R)(?=(?s:.*?^(portIndex.+?\R)\R))|(?s)^portIndex.+?\R(?=\R)
REPLACE
\2?1\1
You should obtain the expected OUTPUT text :
portIndex: 130 portName: slot1 port18 portState: 1Online 10:00:00:90:fe:c0:60:91 portIndex: 131 portName: slot1 port19 portState: 1Online 10:00:00:10:3e:fa:65:88 portIndex: 131 portName: slot1 port19 portState: 1Online 10:00:00:10:3f:fb:66:99 portIndex: 131 portName: slot1 port19 portState: 1Online 21:00:00:1a:1b:b5:9b:80 portIndex: 132 portName: slot1 port20 portState: 1Online 10:00:00:10:4a:bb:d9:6e portIndex: 132 portName: slot1 port20 portState: 1Online 10:00:00:10:4a:ba:52:b9 portIndex: 243 portName: slot12 port19 portState: 2Offline
=> This second regex :
-
Adds the
3
lines of code, of the current section, before repeating each line of hexadecimal characters, if any -
Deletes the temporary
3
lines of code at the end of each section
Best Regards,
guy038
-