Saturday, April 27, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 601  / 3 Years ago, sun, october 31, 2021, 3:56:33

I have the following list that I want to break on the digit. For example:



From:



103Ru
103mRh
104
1041


To:



103
Ru
103
mRh
104
1041


I would like to use Regx with sed or maybe awk in order to achieve this result. But most of my approaches failed. I need some advice or possibly some solution. Thanks


More From » sed

 Answers
1
$ sed -r 's/([0-9])([^0-9])/1
2/g' filename
103
Ru
103
mRh
104
1041


The above regex looks for a number followed by not a number. If found, it inserts a newline between them.



In more detail, sed commands of the form s/old/new/ look for old and replace it with new. In our case, old consists of two characters: ([0-9]) matches any number and, because it is enclosed in parens, it saves the value. ([^0-9]) matches anything other than a number and saves it also. Those two characters, if found, are replaced with 1
2
which means the first match (the number), a newline, and the second match (not-a-number).



MORE: If we want to break at the beginnings of numbers as well as at the end, then we add one more substitution command:



$ echo xyz541wpk | sed -r 's/([0-9])([^0-9])/1
2/g; s/([^0-9])([0-9])/1
2/g'
xyz
541
wpk


The second substitution command is just like the first but it looks for the reverse pattern: not-a-number followed by a number.


[#26314] Monday, November 1, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aciousoun

Total Points: 178
Total Questions: 110
Total Answers: 98

Location: Lithuania
Member since Fri, Sep 4, 2020
4 Years ago
;