Insert string/words above to lines contain numbers
-
Hello, @isaac-goh, @peterjones and All,
Ah OK ! So, now, we have to find out something less restrictive, because :
-
The articles may contain digits or an underscore in their name
-
The quantities may contain the symbol
:
In order that the regexes clearly recognize these two entities, I assume that the articles will always begin with an upper or lower letter !
So, from this INPUT text :
Orange123 50:00 20:00 apple234 30:00 20:00 10:00 pear456 50:00 20:00 21:00 10:00
The first regex S/R to use becomes :
-
SEARCH
^([\u\l]\w*\R)((?:[\d:]+(?:\R|(\Z)))+)
-
REPLACE
$2(?3\r\n)$1
or$2(?3\n)$1
if you use Unix files
and gives :
50:00 20:00 Orange123 30:00 20:00 10:00 apple234 50:00 20:00 21:00 10:00 pear456
Then, the second regex S/R to use becomes :
-
SEARCH
^([\d:]+\R)(?=(?s:.*?)([\u\l]\w*)$)|^[\u\l]\w*\R
-
REPLACE
?1$2\x20$1
and gives the expected OUTPUT text :
Orange123 50:00 Orange123 20:00 apple234 30:00 apple234 20:00 apple234 10:00 pear456 50:00 pear456 20:00 pear456 21:00 pear456 10:00
Jut tell me if additional symbols must appear, apart from the
:
character, in the quantities !Best Regards,
guy038
-
-
@guy038
Yes additional symbols like underscore _ and additional colon : as shown below. Many thanks.Convert from this:
Orange123_432
50:00:00
20:00:00
apple234_678
30:00:00
20:00:00
10:00:00
pear456_321
50:00:00
20:00:00
21:00:00
10:00:00To this:
Orange123_432 50:00:00
Orange123_432 20:00:00
apple234_678 30:00:00
apple234_678 20:00:00
apple234_678 10:00:00
pear456_321 50:00:00
pear456_321 20:00:00
pear456_321 21:00:00
pear456_321 10:00:00Best Regards
-
My apology as there are more than 3000+ lines. I realised additional symbols like underscore _ additional colon : and alphanumeric as shown below. Many thanks.
Convert from this:
Orange123_432
50:00:00:1e
20:00:00:1e
apple234_678
30:00:00:1a
20:00:00:1a
10:00:00:1a
pear456_321
50:00:00:1b
20:00:00:1b
21:00:00:1b
10:00:00:1bTo this:
Orange123_432 50:00:00:1e
Orange123_432 20:00:00:1e
apple234_678 30:00:00:1a
apple234_678 20:00:00:1a
apple234_678 10:00:00:1a
pear456_321 50:00:00:1b
pear456_321 20:00:00:1b
pear456_321 21:00:00:1b
pear456_321 10:00:00:1bBest Regards
-
So what is the rule you use to decide which is the “header” line and which is the “other” lines? Is the rule “if it starts with a letter (uppercase or lowercase), it is a header line; if it starts with a number, it is an ‘other’ line”? Or something else.
You keep on changing what you expect. If you do not give a reasonable definition of what you want, you cannot expect that someone would get it right for you.
----
Useful References
-
Hi, @isaac-goh, @peterjones and All,
Well. So :
-
The articles contain letters, may contain digits or an underscore in their name and begin with a letter
-
The quantities contains digits, may contain the
:
symbol and may have a trailing lower-case letter
In this case, use successively these two regex S/R, below :
-
SEARCH
^([\u\l]\w*\R)((?:[\d:]+\l?(?:\R|(\Z)))+)
-
REPLACE
$2(?3\r\n)$1
or$2(?3\n)$1
if you use Unix files
and :
-
SEARCH
^([\d:]+\l?\R)(?=(?s:.*?)^([\u\l]\w*)$)|^[\u\l]\w*\R
-
REPLACE
?1$2\x20$1
Which will change the INPUT test :
Orange123_432 50:00:00:1e 20:00:00:1e apple234_678 30:00:00:1a 20:00:00:1a 10:00:00:1a pear456_321 50:00:00:1b 20:00:00:1b 21:00:00:1b 10:00:00:1b
into the expected OUTPUT text :
Orange123_432 50:00:00:1e Orange123_432 20:00:00:1e apple234_678 30:00:00:1a apple234_678 20:00:00:1a apple234_678 10:00:00:1a pear456_321 50:00:00:1b pear456_321 20:00:00:1b pear456_321 21:00:00:1b pear456_321 10:00:00:1b
BR
guy038
-
-
@guy038
Apology for my late reply as I am out of town for almost a week and need to scan thru more than 3000+ lines. This is what I have gathered as the possible longest lines. -
Convert from this:
hq12orange1_HBA0_Max_123_PG1_FA1D30
21:00:00:0b:1f:c1:f6:ee
50:00:09:1a:50:06:1b:1f
10:00:00:10:ca:b1:60:94
H600_654342_3B_hq11apple6_HBA4
50:06:0b:50:12:45:fa:20
21:00:12:40:0d:c2:4b:75
VICTOR_200_BE_E3A1BC00_MAX_123_FA1D1
50:00:15:42:c1:32:21:1a
50:00:19:45:d1:06:14:04To this:
hq12orange1_HBA0_Max_123_PG1_FA1D30 21:00:00:0b:1f:c1:f6:ee
hq12orange1_HBA0_Max_123_PG1_FA1D30 50:00:09:1a:50:06:1b:1f
hq12orange1_HBA0_Max_123_PG1_FA1D30 10:00:00:10:ca:b1:60:94
H600_654342_3B_hq11apple6_HBA4 50:06:0b:50:12:45:fa:20
H600_654342_3B_hq11apple6_HBA4 21:00:12:40:0d:c2:4b:75
VICTOR_200_BE_E3A1BC00_MAX_123_FA1D1 50:00:15:42:c1:32:21:1a
VICTOR_200_BE_E3A1BC00_MAX_123_FA1D1 50:00:19:45:d1:06:14:04Best Regards
-
So this isn’t a “data conversion service”.
The way it works is you get a couple of “freebies” and then that stops and you are expected to learn from what you have been given.
Of course you may ask questions about any small points, but in general you are expected to do your own work.
Also you may show what you’ve tried and explain how it wasn’t successful and ask for hints, but you may no longer ask for full solutions. -
Hi, @isaac-goh, @peterjones, @alan-kilborn and All,
Of course, @alan-kilborn’s comments are pertinent and you should investigate a bit in regexes : refer to the USEFUL REFERENCES part at the end of the first post of @peterjones !
Now, out of curiosity, I wanted to know, why you asked me, again. Seemingly, my provided regexes did not match all your cases !
Oh…, everything is clear, now ! Really, @isaac-goh, you should have told me, from the very beginning that :
-
The header lines are a range of word characters
\w
, so digits, letters, whatever its case or underscore ) -
Each subsequent line is a range of hexadcimal digits
[[:xdigit:]]
, separated with a colon char
This completly change the regexes, of course !
So from this INPUT text :
hq12orange1_HBA0_Max_123_PG1_FA1D30 21:00:00:0b:1f:c1:f6:ee 50:00:09:1a:50:06:1b:1f 10:00:00:10:ca:b1:60:94 H600_654342_3B_hq11apple6_HBA4 50:06:0b:50:12:45:fa:20 21:00:12:40:0d:c2:4b:75 VICTOR_200_BE_E3A1BC00_MAX_123_FA1D1 50:00:15:42:c1:32:21:1a 50:00:19:45:d1:06:14:04
with this first regex S/R :
-
SEARCH
^(\w+\R)((?:[[:xdigit:]:]+(?:\R|(\Z)))+)
-
REPLACE
$2(?3\r\n)$1
or$2(?3\n)$1
if you use Unix files
It’ll gives you this text :
21:00:00:0b:1f:c1:f6:ee 50:00:09:1a:50:06:1b:1f 10:00:00:10:ca:b1:60:94 hq12orange1_HBA0_Max_123_PG1_FA1D30 50:06:0b:50:12:45:fa:20 21:00:12:40:0d:c2:4b:75 H600_654342_3B_hq11apple6_HBA4 50:00:15:42:c1:32:21:1a 50:00:19:45:d1:06:14:04 VICTOR_200_BE_E3A1BC00_MAX_123_FA1D1
Then, with the second regex S/R, below :
-
SEARCH
^([[:xdigit:]:]+\R)(?=(?s:.*?)^(\w+)$)|^\w+\R
-
REPLACE
?1$2\x20$1
You’ll get the expected OUTPUT text :
hq12orange1_HBA0_Max_123_PG1_FA1D30 21:00:00:0b:1f:c1:f6:ee hq12orange1_HBA0_Max_123_PG1_FA1D30 50:00:09:1a:50:06:1b:1f hq12orange1_HBA0_Max_123_PG1_FA1D30 10:00:00:10:ca:b1:60:94 H600_654342_3B_hq11apple6_HBA4 50:06:0b:50:12:45:fa:20 H600_654342_3B_hq11apple6_HBA4 21:00:12:40:0d:c2:4b:75 VICTOR_200_BE_E3A1BC00_MAX_123_FA1D1 50:00:15:42:c1:32:21:1a VICTOR_200_BE_E3A1BC00_MAX_123_FA1D1 50:00:19:45:d1:06:14:04
Best Regards,
guy038
-
-
@guy038 said in Insert string/words above to lines contain numbers:
?1$2\x20$1
Your superb effort is much appreciated. Many thanks.