Sponsored Content
Top Forums Shell Programming and Scripting Bash 3.2 - Array / Regex - IF 3rd member in array ends in 5 digits then do somthing... Post 302847233 by neutronscott on Monday 26th of August 2013 01:28:21 PM
Old 08-26-2013
If you really want to use bash's regex matching, to handle differences in bash versions you'd want to use a variable to hold the regex.

Code:
#!/bin/bash
re='[[:digit:]]{5}$'
a=(0 1 2 12345 1234)

if [[ ${a[3]} =~ $re ]]; then
  echo true
fi

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

perl: storing regex in array variables trouble

hi this is an example of code: use strict; use warnings; open FILE, "/tmp/result_2"; my $regex="\\ Starting program ver. (.*)"; my $res="Program started, version <$1> - OK.\n"; while (<FILE>) { if ($_ =~ /($regex)/) { print "$res"; } } close FILE; This finds $regex and print... (3 Replies)
Discussion started by: xist
3 Replies

2. Programming

Creating an array to hold posix thread ids: Only dynamic array works

I am facing a strange error while creating posix threads: Given below are two snippets of code, the first one works whereas the second one gives a garbage value in the output. Snippet 1 This works: -------------- int *threadids; threadids = (int *) malloc (num_threads * sizeof(int)); ... (4 Replies)
Discussion started by: kmehta
4 Replies

3. Shell Programming and Scripting

How do I create an array from a file using every 3rd line

A file contains the following information shown below. Every ceName has 2 consecutive lines that have to be evaluated, using awk, sed, cut (any common unix tools). Input file: ceName: Node-1 processName: tzMgmt Status: PROCESS_NOT_RUNNING ceName: Node-2 processName: tzMgmt Status:... (15 Replies)
Discussion started by: BRH
15 Replies

4. Shell Programming and Scripting

Grep: Searching with a regex that contains a variable from an array

I'm attempting to grep for lines formatted like this: grep -e '^\\",' Any suggestions as to why this isn't working? ---------- Post updated at 05:03 PM ---------- Previous update was at 04:17 PM ---------- This was my solution: grep -e '^\'\",' It's hard to read, but basically I... (4 Replies)
Discussion started by: AcerAspirant
4 Replies

5. Shell Programming and Scripting

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... (4 Replies)
Discussion started by: Ophiuchus
4 Replies

6. Shell Programming and Scripting

Use decimal value of array in bc ends with illegal character

hi all I have to read a long cvs file every 4 columns with decimal "3,45" numbers. The 9th row in this cvs is the first line I need, so it I tail -n+9. I use sed -e 's/,/./g' to get decimal values with . delimiter. So far no problem. Goal is to get two maximum negative forces in ranges... (5 Replies)
Discussion started by: Grille
5 Replies

7. Shell Programming and Scripting

Bash arrays: rebin/interpolate smaller array to large array

hello, i need a bit of help on how to do this effectively in bash without a lot of extra looping or massive switch/case i have a long array of M elements and a short array of N elements, so M > N always. M is not a multiple of N. for case 1, I want to stretch N to fit M arrayHuge H = (... (2 Replies)
Discussion started by: f77hack
2 Replies

8. Shell Programming and Scripting

Bash Array connectin to another Array

Hello, i have a script that i need account_number to match a name. for exsample : ACCOUNT_ID=(IatHG8DC7mZbdymSoOr11w KbnlG2j-KRQ0-1_Xk356s8) and i run a loop curl requst with this the issue is that i want to know on which account were talking about so bash will know this : ... (4 Replies)
Discussion started by: batchenr
4 Replies

9. Shell Programming and Scripting

Fastest alternatives to flattening a non-uniform nested array with regex?

Hello, I'm looking at simplfying a function that flattens an array, it uses recursion and filters objects by type but this seems to be a waste of resources to me at least, using conditionals like this seems like a bad idea. The array can be a generic type, int, string, float but not some... (2 Replies)
Discussion started by: f77hack
2 Replies

10. Shell Programming and Scripting

Sending an array member to file

This may be a dupe, but the problem is not complex,it is simple issue. I can create an array of string(s) located in positioning parameters for further processing. Works as expected. Now I like to send / write each string , space separated words , into a file. Of course I tried... (4 Replies)
Discussion started by: annacreek
4 Replies
SPLITI(3)								 1								 SPLITI(3)

spliti - Split string into array by regular expression case insensitive

SYNOPSIS
array spliti (string $pattern, string $string, [int $limit = -1]) DESCRIPTION
Splits a $string into array by regular expression. This function is identical to split(3) except that this ignores case distinction when matching alphabetic characters. Warning This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged. PARAMETERS
o $pattern - Case insensitive regular expression. If you want to split on any of the characters which are considered special by regular expressions, you'll need to escape them first. If you think spliti(3) (or any other regex function, for that matter) is doing something weird, please read the file regex.7, included in the regex/ subdirectory of the PHP distribution. It's in manpage for- mat, so you'll want to do something along the lines of man /usr/local/src/regex/regex.7 in order to read it. o $string - The input string. o $limit - If $limit is set, the returned array will contain a maximum of $limit elements with the last element containing the whole rest of $string. RETURN VALUES
Returns an array of strings, each of which is a substring of $string formed by splitting it on boundaries formed by the case insensitive regular expression $pattern. If there are n occurrences of $pattern, the returned array will contain n+1 items. For example, if there is no occurrence of $pattern, an array with only one element will be returned. Of course, this is also true if $string is empty. If an error occurs, spliti(3) returns FALSE. EXAMPLES
This example splits a string using 'a' as the separator : Example #1 spliti(3) example <?php $string = "aBBBaCCCADDDaEEEaGGGA"; $chunks = spliti ("a", $string, 5); print_r($chunks); ?> The above example will output: Array ( [0] => [1] => BBB [2] => CCC [3] => DDD [4] => EEEaGGGA ) NOTES
Note As of PHP 5.3.0, the regex extension is deprecated in favor of the PCRE extension. Calling this function will issue an E_DEPRECATED notice. See the list of differences for help on converting to PCRE. Tip spliti(3) is deprecated as of PHP 5.3.0. preg_split(3) with the i ( PCRE_CASELESS) modifier is the suggested alternative. SEE ALSO
preg_split(3), split(3), explode(3), implode(3). PHP Documentation Group SPLITI(3)
All times are GMT -4. The time now is 03:57 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy