Regular Expressions in shell scripts <yawn>


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Regular Expressions in shell scripts <yawn>
# 1  
Old 09-04-2001
Regular Expressions in shell scripts <yawn>

Quite possibly a simple problem who's answer is evading me:

I am trying to write a sh shell script, part of which is *logically* trying to do the following:

if [ $1 = [A-Za-z] ]; then
...
fi

if [ $1 = [0-9.] ]; then
...
else
...
fi

Where the 1st condition is looking for a hostname passed as $1, the second a dotted-quad ip address, and the 3rd a catch-all error condition.

I cannot work out how to use regex with the test ([) command. Would $1 always evaluate to "TRUE"? And therefore I would use something like:

if [ $1 -a regex ]; then
...
fi

or not?
BTW: I know the regex I have given in the example are not water-tight for their application ([0-9.] doesn't necessarily guarantee a dotted-quad by any means), this is a tool which is only going to be used by myself, and therefore isn't too much of a problem

If someone would like to supply the regex for a dotted quad I would be most greatful :-), but no sweat.

Thanks in advance.
# 2  
Old 09-04-2001
Dotted Quad regex

Is this the correct regex for a dotted-quad IP address?

[0-9]\{1,3\}\.\{1\}[0-9]\{1,3\}\.\{1\}[0-9]\{1,3\}\.\{1\}[0-9]\{1,3\}
# 3  
Old 09-04-2001
In ksh you can use patterns...
Code:
if [[ $1 = +([0-9]).+([0-9]).+([0-9]).+([0-9]) ]] ; then
   echo $1 is an ip address
else
   echo $1 is not an ip address
fi

# 4  
Old 09-04-2001
stright from Mastering Regular Expresstions by Oreilly page 124

Code:
^([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.
([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.$

this should all be on 1 line btw. the reason 0-9 wont work as mentioned above it cuz if you want a ligit ip all the time 0-9 wont work. remember 0-9 says even an ip of 999.999.999.999 is valid when we all know is not valid in any universe (yet anyways).

btw this book is a book i think everyone that is into unix, programing, scripting should read.

the hostname check is on page 167

Last edited by Optimus_P; 09-04-2001 at 06:38 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regular Expressions in K Shell Script

I need to write a K shell script to find full file names , line numbers and lines which have words meeting either of following 2 criterias - 1)words which are 6 to 8 character long and alphanumeric. 2)Minimum 8 characters, one upper case, one lower case letter, one of the special characters... (1 Reply)
Discussion started by: Rajpreet1985
1 Replies

2. Shell Programming and Scripting

Regular Expressions

what elements does " /^/ " match? I did the test which indicates that it matches single lowercase character like 'a','b' etc. and '1','2' etc. But I really confused with that. Because, "/^abc/" matches strings like "abcedf" or "abcddddee". So, what does caret ^ really mean? Any response... (2 Replies)
Discussion started by: DavidHe
2 Replies

3. Shell Programming and Scripting

Regular Expressions

#!/usr/bin/perl $word = "one last challenge"; if ( $word =~ /^(\w+).*\s(\w+)$/ ) { print "$1"; print "\n"; print "$2"; } The output shows that "$1" is with result one and "$2" is with result challenge. I am confused about how this pattern match expression works step by step. I... (8 Replies)
Discussion started by: DavidHe
8 Replies

4. UNIX for Dummies Questions & Answers

Regular expressions

In regular expressions with grep(or egrep), ^ works if we want something in starting of line..but what if we write ^^^ or ^ for pattern matching??..Hope u all r familiar with regular expressions for pattern matching.. (1 Reply)
Discussion started by: aadi_uni
1 Replies

5. UNIX for Advanced & Expert Users

regular expressions

I have a flat file with the following drug names Nutropin AQ 20mg PEN Cart 2ml Norditropin Cart 15mg/1.5ml I have to extract digits that are before mg i.e 20 and 15 ; how to do this using regular expressions Thanks ram (1 Reply)
Discussion started by: ramky79
1 Replies

6. UNIX for Dummies Questions & Answers

Execute a shell script using regular expressions

I am have a configuration script that my shell script uses. There is a regular expression defined for the input file. How do execute the shell script and pass the name of the input file using a regular expression. I would greatly appreciate some help. If you could point my to a website that... (1 Reply)
Discussion started by: supergirl3954
1 Replies

7. UNIX for Dummies Questions & Answers

regular expressions

Hi Gurus, I need help with regular expressions. I want to create a regular expression which will take only alpha-numeric characters for 7 characters long and will throw out an error if longer than that. i tried various combinations but couldn't get it, please help me how to get it guys. ... (2 Replies)
Discussion started by: ragha81
2 Replies

8. Shell Programming and Scripting

Help with regular expressions

I have following content in the file CancelPolicyMultiLingual3=U|PC3|EN RestaurantInfoCode1=U|restID1|1 ..... I am trying to use following matching extression \|(+) to get this PC3|EN restID1|1 Obviously it does not work. Any ideas? (13 Replies)
Discussion started by: arushunter
13 Replies

9. Shell Programming and Scripting

Regular Expressions

How can i create a regular expression which can detect a new line charcter followed by a special character say * and replace these both by a string of zero length? Eg: Input File san.txt hello hi ... (6 Replies)
Discussion started by: sandeep_hi
6 Replies

10. Shell Programming and Scripting

using regular expressions in c shell control structure

i cant get around using regular expressions in if/else statements. it simply doesnt give me the right results. i've tried using switch/case but that is just as sh!tty as well. (pardon my french but im getting frustrated with c shell..only reason why im writing in it is because it's a hwk... (3 Replies)
Discussion started by: ballazrus
3 Replies
Login or Register to Ask a Question