How to insert an array element within regex?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to insert an array element within regex?
# 1  
Old 09-09-2013
How to insert an array element within regex?

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:
Code:
22030046075451230200460754516925090046075451

The code I've got so far it works if I write one regex for each substring like this:
Code:
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.

Code:
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[i](.{10})(.{2})(.{2})?/, t) )
	printf("%s,%s,%s ",p[i],t[1],t[2],t[3]);
}'

the desired output is:
Code:
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.

Last edited by Ophiuchus; 09-10-2013 at 12:04 AM..
# 2  
Old 09-10-2013
Try this,
Code:
echo "22030046075451230200460754516925090046075451" | awk -F "2[2345]" 'BEGIN{p[22]="MJS,"
p[23]="UYT,"
p[24]="WER,"
p[25]="PIL,"
}
{a=substr($0,1,2); printf p[a];for (i=2;i<=NF;i++) {
len=length($i);lntotal+=len+2; if (len >= 12 ) { printf substr($i,1,10)","substr($i,11,2);} if (len == 14 ){ printf ","substr($i,13);} b=substr($0,lntotal+1,2);if(p[b]) { printf ", "p[b]};
} printf "\n";
}'

# 3  
Old 09-10-2013
Hi.

Create a variable with the regular expression characters, then use that variable in the match function.

Best wishes ... cheers, drl

Last edited by drl; 09-10-2013 at 04:21 PM..
# 4  
Old 09-10-2013
Hello pravin27,

Thank you for the help, the issue is that the main awk program has another field separator
and I couldn't include this new FS without affect the rest.

Hello drl,

I've tried put all characters of the regex in a variable but doesn't work, only works when
I write literally the regex within match function.

Thanks for any help.
# 5  
Old 09-10-2013
Hi.

Here's an example:
Code:
#!/usr/bin/env bash

# @(#) s2	Demonstrate awk built-in function match(target,re-string).

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C awk

FILE=${1-data2}

pl " Input data file $FILE:"
cat $FILE

pl " Results:"
awk --posix '
	BEGIN { re = "ab{2}" }
	{ if ( match($0,re) ) { print " Matched",re,"in",$0; next} }
	{ print " NO match for",re,"in",$0}
' $FILE

exit 0

producing:
Code:
$ ./s2

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
awk GNU Awk 3.1.5

-----
 Input data file data2:
a
ab
abb
aab
aabbbb

-----
 Results:
 NO match for ab{2} in a
 NO match for ab{2} in ab
 Matched ab{2} in abb
 NO match for ab{2} in aab
 Matched ab{2} in aabbbb

Note that for some special characters in REs, one must use --posix with gawk.

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to insert a CSV within xml element tag using Python?

Hi Team, I have a CSV file which I have to read through and needs to insert the content within an XML file using Python ONLY ( as most of the code base we have in python only). I managed to find the first part, missing how to insert to XML under "specific" tags. cat input.csv... (0 Replies)
Discussion started by: panyam
0 Replies

2. UNIX for Advanced & Expert Users

Array Element

This question is for someone that's more familiar with Array Element. I need to know if the maximum array element that can be assigned is 1024 and if its so, Is there a workaround solution when the counter exceeded 1024? param_array="$param_nam" counter=$counter+1 #to avoid space... (3 Replies)
Discussion started by: cumeh1624
3 Replies

3. Shell Programming and Scripting

Bash 3.2 - Array / Regex - IF 3rd member in array ends in 5 digits then do somthing...

Trying to do some control flow parsing based on the index postion of an array member. Here is the pseudo code I am trying to write in (preferably in pure bash) where possible. I am thinking regex with do the trick, but need a little help. pesudo code if == ENDSINFIVEINTS ]]; then do... (4 Replies)
Discussion started by: briandanielz
4 Replies

4. Shell Programming and Scripting

ksh insert element in array

Hi all, I need help with the following scenario in ksh. If the number of elements contained by arrayA is 11 I need to insert a zero as the element arrayA then print all arrayA elements separated by comma. Appreciate your help. (9 Replies)
Discussion started by: ejianu
9 Replies

5. Shell Programming and Scripting

Multiplying array element

I am trying to take all the elements of an array and multiply them by 2, and then copy them to a new array. Here is what I have i=0 for true in DMGLIST do let DMGSIZES2="${DMGSIZES}"*2 let i++ done unset i echo ${DMGSIZES2} It does the calculation correctly for the first element,... (7 Replies)
Discussion started by: nextyoyoma
7 Replies

6. Shell Programming and Scripting

remove an element from array

I need to remove an element from the below array variable TABLENAME. #!/bin/ksh set -A TABLENAME "mirf roxar keke mirs" echo "the array is ${TABLENAME}" If i need to remove say keke and have the final TABLENAME as below, how this could be achieved. Pls throw some light. echo "Modified... (3 Replies)
Discussion started by: michaelrozar17
3 Replies

7. Shell Programming and Scripting

HELP unsetting array element in loop

I have a loop and I need to be able to unset the array element that I am currently accessing in it. I was thinking of making a counter that increments with the loop and doing unset $dirs but if I do that I am not sure if the other members of the array would get shifted down in index (meaning that... (2 Replies)
Discussion started by: msf5042
2 Replies

8. Shell Programming and Scripting

Shift array element

I want to delete and 0th element of array in shell scrpit and also shift all others to one level up. (2 Replies)
Discussion started by: darshakraut
2 Replies

9. Shell Programming and Scripting

Reading Of Array Element in Unix

Hi, I am using the following command to initialize and array # arr=ffffff00 # echo $arr ffffff00 my requirement is i want to fetch the one by one character from array. I don't know how to fetch one by one charcter from array. Please i need an help. Thank you very much Ravi R N (2 Replies)
Discussion started by: ravi_rn
2 Replies

10. Shell Programming and Scripting

accessing my first element of array

Hello everyonel, I have an array set like so num=4 read name arr=name I go through while loop to assign different values to different array element from 1 to 4. when I try to access the FIRST element of the array I get the last one first. Like if I say ${arr} it will show the last element... (4 Replies)
Discussion started by: afadaghi
4 Replies
Login or Register to Ask a Question

Featured Tech Videos