Hello to all,
I'm trying to separate the string "str" using a regex within match function.
The substrings that I want to separate, begin with 22, 23, 24 or 25 and followed by 12 or 14 characters. And I want to
replace 22 with MJS, 23 with UYT, 24 with WER and 25 with PIL.
For this string "str", the substrings I want to match are:
22030046075451230200460754516925090046075451
The code I've got so far it works if I write one regex for each substring like this:
if ( match(str, /22(.{10})(.{2})/, t) )
or
if ( match(str, /23(.{10})(.{2})/, t) )
or
if ( match(str, /24(.{10})(.{2})(.{2})/, t) )
or
if ( match(str, /25(.{10})(.{2})/, t) )
But I'm trying to include a "for loop" for each element of array "m[i]" but I don't know how to do it or a better way to do it.
awk 'BEGIN {
str=22030046075451230200460754516925090046075451
p[1]="MJS"
p[2]="UYT"
p[3]="WER"
p[4]="PIL"
m[1]="22"
m[2]="23"
m[3]="24"
m[4]="25"
for(i=1;i<=4;i++)
if ( match(str, /m(.{10})(.{2})(.{2})?/, t) )
printf("%s,%s,%s ",p,t[1],t[2],t[3]);
}'
the desired output is:
MJS,0300460754,51 UYT,0200460754,51,69 PIL,0900460754,51
PS: I want to do it in awk, because the string "str" is get inside a major awk script.
Thanks in advance for any help.