Visit Our UNIX and Linux User Community


BASH regex (convert from working perl version)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH regex (convert from working perl version)
# 1  
Old 10-13-2009
BASH regex (convert from working perl version)

Hi there, I need to test that a variable ($VAR) matches a regex mask in BASH. I have the exact thing working in perl (below), but could somebody advise me how i would do the same in BASH ? do i need to use something like egrep ?


Code:
#!/bin/perl -w

my $VAR = "some value";

if ( $VAR =~ /^[a-zA-Z]+?=[\.\/a-zA-Z0-9_-]+?$/ ) {
        print "match\n";
} else {
        print "no match\n";
}

any help on this would be great

---------- Post updated at 05:13 AM ---------- Previous update was at 04:44 AM ----------

its ok ive figured it out using egrep .... Smilie


Code:
#!/bin/bash

VAR="some value"


RESULT=$( echo $VAR | egrep ^[a-zA-Z]+?=[\.\/a-zA-Z0-9_-]+?$ )

if [ -z $RESULT ]; then
        echo bad
else
        echo good
fi


Last edited by rethink; 10-13-2009 at 07:55 AM..
# 2  
Old 10-13-2009
Which bash version? If I recall correctly 3 and above offers support for regular expressions:

Code:
for v in 'ko=a|b' 'ok=0_/'; do
  [[ $v =~ ^[[:alpha:]]+=[./[[:alnum:]_-]+$ ]] &&
    printf '$v is ok: %s\n' "$v" ||
          printf '$v is not ok: %s\n' "$v"
done

The above outputs:

Code:
$v is not ok: ko=a|b
$v is ok: ok=0_/

# 3  
Old 10-13-2009
thanks Radoulov

so if i use my your code in my example (my bash is version is 3.00.16)

Code:
#!/bin/bash

VAR="serial=1234"

[[ $VAR =~ ^[a-zA-Z]+?=[\.\/a-zA-Z0-9_-]+?$ ]] && printf '$VAR is ok: %s\n' "$VAR"  || printf '$VAR is not ok: %s\n' "$VAR"

I get ...

Code:
# /tmp/test.sh
$VAR is not ok: serial=1234

can you see what im doing wrong here ?

m sure the regex is OK, because as you can see from my perl and egrep examples above, it all matches fine ?
# 4  
Old 10-13-2009
Quote:
Originally Posted by rethink
thanks Radoulov

so if i use my your code in my example (my bash is version is 3.00.16)

Code:
#!/bin/bash

VAR="serial=1234"

[[ $VAR =~ ^[a-zA-Z]+?=[\.\/a-zA-Z0-9_-]+?$ ]] && printf '$VAR is ok: %s\n' "$VAR"  || printf '$VAR is not ok: %s\n' "$VAR"

Did you notice that I changed the regex?
The Perl regular expressions are more powerful, in bash you don't have (and you don't need it in this case) the non-greedy operator ?.
You also don't need to escape the characters inside the character class.


Quote:
I get ...

Code:
# /tmp/test.sh
$VAR is not ok: serial=1234

can you see what im doing wrong here ?

m sure the regex is OK, because as you can see from my perl and egrep examples above, it all matches fine ?
Try modifying the regular expression as in my previous post.
# 5  
Old 10-13-2009
thanks for that Radoulov

Previous Thread | Next Thread
Test Your Knowledge in Computers #502
Difficulty: Medium
If a function uses a particular process or algorithm such as a Fast Fourier Transform to perform an operation, it would not be appropriate to document it in a series of comments in the source code.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert bash to simple perl

please delete! (0 Replies)
Discussion started by: SkySmart
0 Replies

2. Shell Programming and Scripting

Perl, RegEx - Help me to understand the regex!

I am not a big expert in regex and have just little understanding of that language. Could you help me to understand the regular Perl expression: ^(?!if\b|else\b|while\b|)(?:+?\s+){1,6}(+\s*)\(*\) *?(?:^*;?+){0,10}\{ ------ This is regex to select functions from a C/C++ source and defined in... (2 Replies)
Discussion started by: alex_5161
2 Replies

3. Shell Programming and Scripting

How to make working this regex in perl?

Hello to all, The Regex below is supposed to match all strings except RR45. I've tested in regex101.com and it works, butwhen I try to use it with the perl command below I get the error shown. Regex=(?<=^|RR45)(?!RR45).+?(?=RR45|$) How to fix this? I'm using Cygwin. $ echo... (9 Replies)
Discussion started by: Ophiuchus
9 Replies

4. Shell Programming and Scripting

Convert posix regex to pcre (perl)

In order to clean file from html tags i used the following sed 's/<*>//g' filename Right now i need to do the same from php script so i have to use pcre. How to convert? (1 Reply)
Discussion started by: urello
1 Replies

5. Shell Programming and Scripting

Convert .../($var1|$var2)/... regex to string

Hello, In one part of my shell program I need to translate many of lines in the following pattern : /(folder1|...|folderN)/(sub1|...|subN)/.../(file1|...|fileN) into strings : /folder1/sub1/.../file1 /folder1/sub1/.../... /folder1/sub1/.../fileN ... /folderN/subN/.../fileN the... (2 Replies)
Discussion started by: Ghadamyari
2 Replies

6. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

7. Shell Programming and Scripting

bash with: if, elif & regex not working

Why is only hello3 being printed? There must be some kind of syntax problem because the file list definitely includes all the file extensions line by line. #!/bin/bash find '/home/myuser/folder/' -name '*.c' -type f | while read F do if ] # if the file name ends in .txt.c then ... (6 Replies)
Discussion started by: cyler
6 Replies

8. Shell Programming and Scripting

***convert from perl to bash***

can any body translate the follwing script into one that works in bash? #!/usr/bin/perl # classify_books.pl my $csv_file = shift; my %categories = ( 'childrens' => 'childrens_books.txt', 'horror' => 'horror_books.txt', 'sports ' =>... (3 Replies)
Discussion started by: ferrycorsten73
3 Replies

9. Shell Programming and Scripting

Help! Need to convert bash shell to perl

Hello All. I am very new to Linux and I am currently interning. I have been working on a project for 2 weeks now and I have had no success. I have to convert bash shell into perl to decrypt and store files. Here is the code in Linux and Bash. Any help would be greatly appreciated. $... (0 Replies)
Discussion started by: freak
0 Replies

10. UNIX for Dummies Questions & Answers

Intern needing to convert bash to perl.

Hello All. I am very new to Linux and I am currently interning. I have been working on a project for a week and I have had no success. I have to convert bash shell into perl to decrypt and store files. Here is the code in Linux and Bash. Any help would be greatly appreciated. $... (0 Replies)
Discussion started by: freak
0 Replies

Featured Tech Videos