0

I have the following sequence in a file:

AA
MUi
MUi-j
MUi-N
ABk
ABk-l
ABk-N
MBk(i)
MBk-l(i)
MBk-l(i-j)
MBk-l(i-N)
MBk-N(i-j)
MBk-N(i-N)

I want to split it in small pieces: The first two chars will always exist, the rest are optional. i,j,k,l are numbers that can vary from 1 to 191. N is the character "N". I want to have the following result:

AA
MU i
MU i j
MU i N
AB k
AB k l
AB k N
MB k i
MB k l i
MB k l i j
MB k l i N
MB k N i j
MB k N i N

Can this be done with AWK patsplit?

2

Via sed:

sed 's/^../& /;s/[-()]/ /g' datafile.dat
2

With GNU awk:

$ gawk -vFPAT='[A-Z]+|[a-z]' 'NF+=0' file
AA
MU i
MU i j
MU i N
AB k
AB k l
AB k N
MB k i
MB k l i
MB k l i j
MB k l i N
MB k N i j
MB k N i N

Note that will skip empty lines, since it relies on a non-zero side-effect value of NF+=0 to trigger the default print; if that's undesirable, you can use the slightly more verbose {NF+=0} 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.