Sponsored Content
Top Forums Shell Programming and Scripting How to insert an array element within regex? Post 302851831 by Ophiuchus on Monday 9th of September 2013 10:18:09 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
set_color(1)							       fish							      set_color(1)

NAME
set_color - set_color - set the terminal color set_color - set the terminal color Synopsis set_color [-v --version] [-h --help] [-b --background COLOR] [COLOR] Description Change the foreground and/or background color of the terminal. COLOR is one of black, red, green, brown, yellow, blue, magenta, purple, cyan, white and normal. o -b, --background Set the background color o -c, --print-colors Prints a list of all valid color names o -h, --help Display help message and exit o -o, --bold Set bold or extra bright mode o -u, --underline Set underlined mode o -v, --version Display version and exit Calling set_color normal will set the terminal color to whatever is the default color of the terminal. Some terminals use the --bold escape sequence to switch to a brighter color set. On such terminals, set_color white will result in a grey font color, while set_color --bold white will result in a white font color. Not all terminal emulators support all these features. This is not a bug in set_color but a missing feature in the terminal emulator. set_color uses the terminfo database to look up how to change terminal colors on whatever terminal is in use. Some systems have old and incomplete terminfo databases, and may lack color information for terminals that support it. Download and install the latest version of ncurses and recompile fish against it in order to fix this issue. Version 1.23.1 Sun Jan 8 2012 set_color(1)
All times are GMT -4. The time now is 02:29 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy