Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Need help with Regex for bash Post 302749909 by newbie2010 on Sunday 30th of December 2012 02:22:26 AM
Old 12-30-2012
Need help with Regex for bash

Hi, I am trying to match this word: hexagon-bx.mydomain.com with regex. I have tried this:

Code:
"[a-z0-9\-\[bu]\.*]*$"

So far I have not been successful. I also need to make sure that the regex will match words that just have lowercase letters and numbers in them, such as camera01. How can I create such an expression? I am trying to do a expr match on "[a-z0-9\-\[bu]\.*]*$" so that it would pick up words like hexagon-bx/mydomain.com. Any help is greatly appreciated!

Last edited by radoulov; 12-30-2012 at 09:09 AM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

regex test in bash

Hi I want to do a regex test and branch based on the test result, but this doesn't seems to work :confused: if \) ]] then echo success else echo failed fi (1 Reply)
Discussion started by: subin_bala
1 Replies

2. Shell Programming and Scripting

bash regex =~ case insensetive, possible?

It can get very annoying that bash regex =~ is case-sensetive, is there a way to set it to be case-insensetive? if ]; then echo match else echo no match fi (8 Replies)
Discussion started by: TehOne
8 Replies

3. Shell Programming and Scripting

Bash string replacement - how to use regex?

Hello I have a bash script where I need to do a substring replacement like this: variable2=${variable1/foo/bar} However, I only want "foo" replaced if it is at the end of the line. However, this does not work: variable2=${variable1/foo$/bar} as you can see I'm using the $ regex for... (2 Replies)
Discussion started by: Ubuntu-UK
2 Replies

4. Shell Programming and Scripting

Bash regex

Hello everybody, I'm clearly not an expert in bash scripting as I've written maybe less than 10 scripts in my life. I'm trying to strip an xml string removing every tag in it. I'm using bash substitution to do so, but apparently I missed something about what is a regex for bash ... As an... (4 Replies)
Discussion started by: kerloi
4 Replies

5. Shell Programming and Scripting

[BASH] Allow name with spaces (regex)

Hey all, I have a very simple regular expression that I use when I want to allow only letters with spaces. (I know this regex has a lot of shortcomings, but I'm still trying to learn them) isAlpha='^*$'However, when I bring this over to BASH it doesn't allow me to enter spaces. I use the... (3 Replies)
Discussion started by: whyte_rhyno
3 Replies

6. Shell Programming and Scripting

Bash regex help

I've been using the following regex below in a bash script on RHEL 5.5 using version GNU bash, version 3.2.25(1)-release I've tried using the script on RHEL 6.3 which uses GNU bash, version 4.1.2(1)-release I assume there's been alot of changes to bash since that's quite a jump in revisions.... (12 Replies)
Discussion started by: woodson2
12 Replies

7. Shell Programming and Scripting

Hi im new to bash scripting I want to know what does the regex expression do ??

# check host value regex='^(||1|2|25)(\.(||1|2|25)){3}$' if ')" != "" ]; then if ]; then echo host $host not found exit 4 fi elif ]; then echo $host is an invalid host address exit 5 fi (1 Reply)
Discussion started by: kevin298
1 Replies

8. UNIX for Dummies Questions & Answers

Regex for (a|b) in bash

I am trying to find files using the following by using simple bash script: if -2014 ]]; then echo "yes";fi What I need to find are any files with date 08-**-2014 so August 2014 any files. I can use if -2014 ]]; then echo "yes";fi That works fine. How do I get files beginning with 08... (1 Reply)
Discussion started by: newbie2010
1 Replies

9. Shell Programming and Scripting

Bash regex evaluation not workin

I'm building a script that may received start and end date as parameters. I whant to make it as flexible as possible so I'm accepting epoch and date in a way that "date --date=" command may accept. In order to know if parameter provided is an epoc or a "date --date=" string I evaluate if the value... (2 Replies)
Discussion started by: lramirev
2 Replies

10. Shell Programming and Scripting

Using RegEx with variable within bash if [[ ]]

I stumbled upon a problem, which I simplified to this: There is a list of numbers, stored in variable $LIST, lets use `seq 5 25` for demonstration. There is a number that should be compared against this list. For demonstration I use user input - read VALUE I am trying to compare RegEx... (2 Replies)
Discussion started by: Zorbeg
2 Replies
regex(1F)                                                          FMLI Commands                                                         regex(1F)

NAME
regex - match patterns against a string SYNOPSIS
regex [-e] [ -v "string"] [ pattern template] ... pattern [template] DESCRIPTION
The regex command takes a string from the standard input, and a list of pattern / template pairs, and runs regex() to compare the string against each pattern until there is a match. When a match occurs, regex writes the corresponding template to the standard output and returns TRUE. The last (or only) pattern does not need a template. If that is the pattern that matches the string, the function simply returns TRUE. If no match is found, regex returns FALSE. The argument pattern is a regular expression of the form described in regex(). In most cases, pattern should be enclosed in single quotes to turn off special meanings of characters. Note that only the final pattern in the list may lack a template. The argument template may contain the strings $m0 through $m9, which will be expanded to the part of pattern enclosed in ( ... )$0 through ( ... )$9 constructs (see examples below). Note that if you use this feature, you must be sure to enclose template in single quotes so that FMLI does not expand $m0 through $m9 at parse time. This feature gives regex much of the power of cut(1), paste(1), and grep(1), and some of the capabilities of sed(1). If there is no template, the default is $m0$m1$m2$m3$m4$m5$m6$m7$m8$m9. OPTIONS
The following options are supported: -e Evaluates the corresponding template and writes the result to the standard output. -v "string" Uses string instead of the standard input to match against patterns. EXAMPLES
Example 1: Cutting letters out of a string To cut the 4th through 8th letters out of a string (this example will output strin and return TRUE): `regex -v "my string is nice" '^.{3}(.{5})$0' '$m0'` Example 2: Validating input in a form In a form, to validate input to field 5 as an integer: valid=`regex -v "$F5" '^[0-9]+$'` Example 3: Translating an environment variable in a form In a form, to translate an environment variable which contains one of the numbers 1, 2, 3, 4, 5 to the letters a, b, c, d, e: value=`regex -v "$VAR1" 1 a 2 b 3 c 4 d 5 e '.*' 'Error'` Note the use of the pattern '.*' to mean "anything else". Example 4: Using backquoted expressions In the example below, all three lines constitute a single backquoted expression. This expression, by itself, could be put in a menu defini- tion file. Since backquoted expressions are expanded as they are parsed, and output from a backquoted expression (the cat command, in this example) becomes part of the definition file being parsed, this expression would read /etc/passwd and make a dynamic menu of all the login ids on the system. `cat /etc/passwd | regex '^([^:]*)$0.*$' ' name=$m0 action=`message "$m0 is a user"`'` DIAGNOSTICS
If none of the patterns match, regex returns FALSE, otherwise TRUE. NOTES
Patterns and templates must often be enclosed in single quotes to turn off the special meanings of characters. Especially if you use the $m0 through $m9 variables in the template, since FMLI will expand the variables (usually to "") before regex even sees them. Single characters in character classes (inside []) must be listed before character ranges, otherwise they will not be recognized. For exam- ple, [a-zA-Z_/] will not find underscores (_) or slashes (/), but [_/a-zA-Z] will. The regular expressions accepted by regcmp differ slightly from other utilities (that is, sed, grep, awk, ed, and so forth). regex with the -e option forces subsequent commands to be ignored. In other words, if a backquoted statement appears as follows: `regex -e ...; command1; command2` command1 and command2 would never be executed. However, dividing the expression into two: `regex -e ...``command1; command2` would yield the desired result. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ SEE ALSO
awk(1), cut(1), grep(1), paste(1), sed(1), regcmp(3C), attributes(5) SunOS 5.10 12 Jul 1999 regex(1F)
All times are GMT -4. The time now is 08:04 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy