Sponsored Content
Top Forums Shell Programming and Scripting BASH regex (convert from working perl version) Post 302361392 by rethink on Tuesday 13th of October 2009 06:13:43 AM
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 06:55 AM..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. Shell Programming and Scripting

Convert bash to simple perl

please delete! (0 Replies)
Discussion started by: SkySmart
0 Replies
Text::Glob(3)						User Contributed Perl Documentation					     Text::Glob(3)

NAME
Text::Glob - match globbing patterns against text SYNOPSIS
use Text::Glob qw( match_glob glob_to_regex ); print "matched " if match_glob( "foo.*", "foo.bar" ); # prints foo.bar and foo.baz my $regex = glob_to_regex( "foo.*" ); for ( qw( foo.bar foo.baz foo bar ) ) { print "matched: $_ " if /$regex/; } DESCRIPTION
Text::Glob implements glob(3) style matching that can be used to match against text, rather than fetching names from a filesystem. If you want to do full file globbing use the File::Glob module instead. Routines match_glob( $glob, @things_to_test ) Returns the list of things which match the glob from the source list. glob_to_regex( $glob ) Returns a compiled regex which is the equivalent of the globbing pattern. glob_to_regex_string( $glob ) Returns a regex string which is the equivalent of the globbing pattern. SYNTAX
The following metacharacters and rules are respected. "*" - match zero or more characters "a*" matches "a", "aa", "aaaa" and many many more. "?" - match exactly one character "a?" matches "aa", but not "a", or "aaa" Character sets/ranges "example.[ch]" matches "example.c" and "example.h" "demo.[a-c]" matches "demo.a", "demo.b", and "demo.c" alternation "example.{foo,bar,baz}" matches "example.foo", "example.bar", and "example.baz" leading . must be explictly matched "*.foo" does not match ".bar.foo". For this you must either specify the leading . in the glob pattern (".*.foo"), or set $Text::Glob::strict_leading_dot to a false value while compiling the regex. "*" and "?" do not match / "*.foo" does not match "bar/baz.foo". For this you must either explicitly match the / in the glob ("*/*.foo"), or set $Text::Glob::strict_wildcard_slash to a false value with compiling the regex. BUGS
The code uses qr// to produce compiled regexes, therefore this module requires perl version 5.005_03 or newer. AUTHOR
Richard Clamp <richardc@unixbeard.net> COPYRIGHT
Copyright (C) 2002, 2003, 2006, 2007 Richard Clamp. All Rights Reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. SEE ALSO
File::Glob, glob(3) perl v5.18.2 2017-10-06 Text::Glob(3)
All times are GMT -4. The time now is 07:57 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy