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
# 1  
Old 04-28-2014
Using BASH =~ regex to match multiple strings

I have a scripting problem that I'm trying to solve, whereby I want to match that a string contains either of three strings. I'm thinking this is probably just me not understanding how to craft the appropriate regex. However, here's what I would like to do:

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

more specifically:

Code:
[[ $STRING =~ "one OR two OR three" ]] && do something || :

I'd like to be able to match based on whether it has one or more of those strings -- or possibly all.

I'm sure this is simple, I just can't get my brain around it.

I know that BASH =~ regex can be system-specific, based on the libs available -- in this case, this is primarily CentOS 6.x (some OSX Mavericks with Macports, but not needed)


Thanks!

Last edited by radoulov; 04-28-2014 at 03:10 PM..
# 2  
Old 04-28-2014
Omit the quotes
Code:
[[ $STRING =~ one|two|three ]] && do-something

While I currently have no clue if it is advisable to quote "$STRING".
# 3  
Old 04-28-2014
Quote:
Originally Posted by MadeInGermany
Omit the quotes
Code:
[[ $STRING =~ one|two|three ]] && do-something

While I currently have no clue if it is advisable to quote "$STRING".
You don't need to do so because neither word splitting nor pathname expansion(globbing) are performed in this context.
This User Gave Thanks to radoulov For This Post:
# 4  
Old 04-28-2014
Quote:
Originally Posted by forrie
I have a scripting problem that I'm trying to solve, whereby I want to match that a string contains either of three strings. I'm thinking this is probably just me not understanding how to craft the appropriate regex. However, here's what I would like to do:

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

more specifically:

Code:
[[ $STRING =~ "one OR two OR three" ]] && do something || :

[...]
When you quote the ERE in <STRING> =~ <ERE>, it's taken literally.
# 5  
Old 04-28-2014
So am I to infer from the responses that what I am attempting to do cannot be done in this context within BASH?

FYI, this does not work:

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

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.

I could do something more verbose like this, but it defeats the purpose of succinct code:

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

or more verbose using:

Code:
if [ "$1" =~ "one" ]
then
  do something
elseif [  "$1" =~ "two" ]
  do another-thing
elseif [ "$1" =~ "three" ]
  do that
else
  echo No match
fi

That's horrible to look at.
# 6  
Old 04-28-2014
Also it would need to be (bash/ksh93/zsh):
Code:
[[ $STRING =~ ^(one|two|three)$ ]] && do-something

or
Code:
if [[ $STRING =~ ^(one|two|three)$ ]]; then
  do_something
fi

or, using a case statement
Code:
case $STRING in 
  one|two|three) do_something
esac

# 7  
Old 04-28-2014
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.
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