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 302846847 by balajesuri on Monday 26th of August 2013 01:58:05 AM
Old 08-26-2013
Code:
#! /bin/bash

x=( 3214 4567 12345 2134 )

echo ${x[2]} | grep -q -E "^[0-9]{5}$"
if [ $? -eq 0 ]
then
    <do_stuff>
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
Tie::Hash::Regex(3pm)					User Contributed Perl Documentation				     Tie::Hash::Regex(3pm)

NAME
Tie::Hash::Regex - Match hash keys using Regular Expressions SYNOPSIS
use Tie::Hash::Regex; my %h; tie %h, 'Tie::Hash::Regex'; $h{key} = 'value'; $h{key2} = 'another value'; $h{stuff} = 'something else'; print $h{key}; # prints 'value' print $h{2}; # prints 'another value' print $h{'^s'}; # prints 'something else' print tied(%h)->FETCH(k); # prints 'value' and 'another value' delete $h{k}; # deletes $h{key} and $h{key2}; or (new! improved!) my $h : Regex; DESCRIPTION
Someone asked on Perlmonks if a hash could do fuzzy matches on keys - this is the result. If there's no exact match on the key that you pass to the hash, then the key is treated as a regex and the first matching key is returned. You can force it to leap straight into the regex checking by passing a qr'ed regex into the hash like this: my $val = $h{qr/key/}; "exists" and "delete" also do regex matching. In the case of "delete" all vlaues matching your regex key will be deleted from the hash. One slightly strange thing. Obviously if you give a hash a regex key, then it's possible that more than one key will match (consider c<$h{qw/./}>). It might be nice to be able to do stuff like: my @vals = $h{$pat}; to get all matching values back. Unfortuately, Perl knows that a given hash key can only ever return one value and so forces scalar context on the "FETCH" call when using the tied interface. You can get round this using the slightly less readable: my @vals = tied(%h)->FETCH($pat); ATTRIBUTE INTERFACE From version 0.06, you can use attributes to define your hash as being tied to Tie::Hash::Regex. You'll need to install the module Attribute::Handlers. METHODS
FETCH Get a value from the hash. If there isn't an exact match try a regex match. EXISTS See if a key exists in the hash. If there isn't an exact match try a regex match. DELETE Delete a key from the hash. If there isn't an exact match try a regex match. AUTHOR
Dave Cross <dave@mag-sol.com> Thanks to the Perlmonks <http://www.perlmonks.org> for the original idea and to Jeff "japhy" Pinyan for some useful code suggestions. COPYRIGHT
Copyright (C) 2001-8, Magnum Solutions Ltd. All Rights Reserved. LICENSE
This script is free software; you can redistribute it and/or modify it under the same terms as Perl itself. SEE ALSO
perl(1). perltie(1). Tie::RegexpHash(1) perl v5.10.0 2008-06-30 Tie::Hash::Regex(3pm)
All times are GMT -4. The time now is 09:38 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy