Using BASH =~ regex to match multiple strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using BASH =~ regex to match multiple strings
# 8  
Old 04-28-2014
Quote:
Originally Posted by forrie
So am I to infer from the responses that what I am attempting to do cannot be done in this context within BASH?
No, not at all...

Quote:

FYI, this does not work:

Code:
[[ "$1" =~ "one|two|three" ]]

That is a string match, not a regex match see other comments...
Quote:
basically, I'm looking to generate a positive result if it matches any of those words.

I've seen other examples (in different context) that will do something:

Code:
[[ "$1" =~ (regex) ]]

but with the emphasis that "regex" is very much platform-dependent (ie: depends highly on the regular expressions libs available on that system.
Where did you get that idea? That is not correct.. It does depend on bash version. Old versions of bash cannot process =~, (but a case statement using unix patterns will work)

------------
Quote:
Originally Posted by forrie
On CentOS 6.5, I am unable to get this to work correctly (per above):

Code:
[[ $STRING =~ ^(one|two|three)$ ]] && do-something

The string will actually be a part of a hostname, for example "one-hostname" or something like that. I need it check that there's a match, possibly doing something based on what it matched. But in this case the match word will be at the beginning ^one.
What happens when you execute:

Code:
STRING=one; [[ $STRING =~ ^(one|two|three)$ ]] && echo hello

in a bash shell?
  • How do you execute your script?

Last edited by Scrutinizer; 04-28-2014 at 03:59 PM..
# 9  
Old 04-28-2014
@forrie,
consider the following:

Code:
$ echo $BASH_VERSION
4.1.10(4)-release
p='^(one|two|three)$'
$ for s in one two three "$p"; do
>   [[ $s =~ $p ]] && printf '%s matches %s\n' "$s" "$p"
>   [[ $s =~ "$p" ]] && printf '%s matches "%s"\n' "$s" "$p"
> done
one matches ^(one|two|three)$
two matches ^(one|two|three)$
three matches ^(one|two|three)$
^(one|two|three)$ matches "^(one|two|three)$"

As I said, when you quote the regular expression, it's taken literally.
As far as I know, the =~ operator is bash version specific
(i.e. it's not available in older bash versions).
There are some other gotchas and some platform specific issues, see the BashWiki for more info (see Portability Considerations).

Last edited by radoulov; 04-28-2014 at 04:10 PM..
# 10  
Old 04-28-2014
I got this to work:

Code:
[[ $STRING =~ ^(one|two|three)$ ]] && echo hello

This also matches a hostname like
Code:
one-hostname

I'm running BASH
Code:
GNU bash, version 4.3.11(1)-release (x86_64-apple-darwin13.1.0)

The problem was user-related :-) I had our internal strings on the brain (dev, prod, test) and was under-caffeinated.

Thank you all for your helpful pointers.
# 11  
Old 05-01-2014
Quote:
Originally Posted by forrie
I got this to work:

Code:
[[ $STRING =~ ^(one|two|three)$ ]] && echo hello

This also matches a hostname like
Code:
one-hostname

I'm running BASH
Code:
GNU bash, version 4.3.11(1)-release (x86_64-apple-darwin13.1.0)

The problem was user-related :-) I had our internal strings on the brain (dev, prod, test) and was under-caffeinated.

Thank you all for your helpful pointers.
If STRING is set to one-hostname, the command:
Code:
[[ $STRING =~ ^(one|two|three)$ ]] && echo hello

should not produce any output. If you're trying to match one, two, or three at the start of the string and accept anything following one of those three strings, you don't want the $. With the $, the RE matches only if one of those three strings is the entire contents of $STRING.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Use strings from nth field from one file to match strings in entire line in another file, awk

I cannot seem to get what should be a simple awk one-liner to work correctly and cannot figure out why. I would like to use patterns from a specific field in one file as regex to search for matching strings in the entire line ($0) of another file. I would like to output the lines of File2 which... (1 Reply)
Discussion started by: jvoot
1 Replies

2. UNIX for Beginners Questions & Answers

How to pass strings from a list of strings from another file and create multiple files?

Hello Everyone , Iam a newbie to shell programming and iam reaching out if anyone can help in this :- I have two files 1) Insert.txt 2) partition_list.txt insert.txt looks like this :- insert into emp1 partition (partition_name) (a1, b2, c4, s6, d8) select a1, b2, c4, (2 Replies)
Discussion started by: nubie2linux
2 Replies

3. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies

4. Shell Programming and Scripting

Nawk match regex of bash variable

Using a bash for loop to pass variables into a nawk loop to capture a string in an sftp log. Tried several different syntax methods to have the variable treated as a regex so the loop will capture the string. for i in `cat /tmp/dar3.out.2` do nawk -vst=$i '$5 ~ /$st/ && /closed/ && /user/... (3 Replies)
Discussion started by: smenago
3 Replies

5. Shell Programming and Scripting

Only Regex pattern match help

Hi We have a tool to monitor logs in our environment. The tool accepts log pattern match only using regex and I accept I am a n00b in that:confused:. I had been banging my head to make it work without much success and at last had to turn on to my last option to post it here. I had got great... (2 Replies)
Discussion started by: radioactive9
2 Replies

6. UNIX for Dummies Questions & Answers

awk to match multiple regex and create separate output files

Howdy Folks, I have a list that looks like this: (file2.txt) AAA BBB CCC DDD and there are 24 of these short words. I am matching these patterns to another file with 755795 lines (file1.txt). I have this code for matching: awk -v f2=file2.txt ' BEGIN { while(... (2 Replies)
Discussion started by: heecha
2 Replies

7. Shell Programming and Scripting

BASH: extracting values from multiple lines after a match

Hi there, I have the following output, # raidctl -l RAID Volume RAID RAID Disk Volume Type Status Disk Status ------------------------------------------------------ c0t1d0 IM OK c0t1d0 OK ... (4 Replies)
Discussion started by: rethink
4 Replies

8. UNIX for Advanced & Expert Users

Regex to match IP address

What do you think of this regex to match IP address? I have been reading up on regex and have seen some really long ones for IP. Would this fail in any scenarios? (+\.){3}* (5 Replies)
Discussion started by: glev2005
5 Replies

9. Shell Programming and Scripting

regex to match basename

Hi Can somebody please help me know how do i match the basename using a regular expression using posix standard in shell script suppose i want to match /u01/Sybase/data/master.dbf the result should be master.dbf as i want to match everything after the last / regards (8 Replies)
Discussion started by: xiamin
8 Replies

10. UNIX for Dummies Questions & Answers

Regex in if-then-else statement to match strings

hello I want to do a pattern match for string in the if statement, but I am not sure how to use regex inside the if statement. I am looking for something like this: if {2,3} ]; then ..... .... ... fi (7 Replies)
Discussion started by: rakeshou
7 Replies
Login or Register to Ask a Question