Regex in Unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regex in Unix
# 1  
Old 10-06-2010
Regex in Unix

user can enter Yes or yes or YES and its captured in variable say var

Code:
if [ $var == [Yy][Ee][Ss] ]
then
  #do something
elif [ $var == [Nn] [Oo] ]
then
  #do something

I think you got what i want to do. I can use "OR" logic in "if" statement like if ["YES"] || [ "yes"] but do not want to use OR , Is there any way it can be done?
# 2  
Old 10-06-2010
hi,
try this.

Code:
echo "Enter Yes or No: "
read a

while true
do
 case $a in
	[Yy]* ) 
 	echo "yes is entered"
 	break;;
	[Nn]* ) 
	echo "no is entered"
 	break;;
 	* ) echo "Please enter Yes or No ";;
 esac
done

# 3  
Old 10-06-2010
You can use typset to force uppercase (or lowercase):

Code:
#typeset -u var
#var=yes
#echo $var
YES

This User Gave Thanks to Klashxx For This Post:
# 4  
Old 10-06-2010
Quote:
Originally Posted by ajincoep
user can enter Yes or yes or YES and its captured in variable say var... I can use "OR" logic in "if" statement like if ["YES"] || [ "yes"] but do not want to use OR , Is there any way it can be done?
Here's an idea - you could convert user input to lower case and then compare it with "yes". Ditto with "no".

Code:
$
$ x="Yes"
$
$ echo $x | tr [A-Z] [a-z]
yes
$
$
$ x="YeS"
$
$ echo $x | tr [A-Z] [a-z]
yes
$
$
$ x="YES"
$
$ echo $x | tr [A-Z] [a-z]
yes
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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. UNIX for Dummies Questions & Answers

read regex from ID file, print regex and line below from source file

I have a file of protein sequences with headers (my source file). Based on a list of IDs (which are included in some of the headers), I'd like to print out only the specified sequences, with only the ID as header. In other words, I'd like to search source.txt for the terms in IDs.txt, and print... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

4. UNIX for Advanced & Expert Users

Vi Regex help

Can someone tell me what is going with this expression :%s/<C-V><C-M>/. Is there a way to get a more useful message if the carriage return has been deleted? http://objectmix.com/editors/149245-fixing-dos-line-endings-within-vim.html#post516826 Why does this expression work for... (1 Reply)
Discussion started by: cokedude
1 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. UNIX for Dummies Questions & Answers

| help | unix | grep (GNU grep) 2.5.1 | advanced regex syntax

Hello, I'm working on unix with grep (GNU grep) 2.5.1. I'm going through some of the newer regex syntax using Regular Expression Reference - Advanced Syntax a guide. ls -aLl /bin | grep "\(x\)" Which works, just highlights 'x' where ever, when ever. I'm trying to to get (?:) to work but... (4 Replies)
Discussion started by: MykC
4 Replies

7. UNIX for Dummies Questions & Answers

regex

I am a newbie. just learning it. I want to create a regex to print the line that has the city, state and the zip code in this form:City, ST 12345 ( Any City, state and a zip code in a file) I tried everything I can, I can't get it to work. Can anyone please help? (3 Replies)
Discussion started by: Pouchie1
3 Replies

8. Shell Programming and Scripting

Regex

How do I write a regular expression to capture the comments of Pascal which are usually delimted by (* and *) or { and }? And also I need a regular expression to express financial quantities in American Notation. They have a leading dollar sign and an optional string of asteriks,a string of decimal... (1 Reply)
Discussion started by: saikrishnan7
1 Replies

9. UNIX for Dummies Questions & Answers

regex

Can anyone give the detailed explanation on regex search i want to know the use of regex in sed and awk also...... the operators like ^,.,* ....etc i need it with some example.....kindly help on this. I gone through the man pages also..but i was not clear......... (1 Reply)
Discussion started by: sivakumar.rj
1 Replies

10. Shell Programming and Scripting

Need a regex

Hi, I am trying to grep for the following type of string from a document given below: 12637 1239 3356 12956 7004 7004 7004 13381 13381 *> 12.0.1.63 0 7018 21872 ? * 208.51.134.254 53 0 3549 7018 21872 ?... (1 Reply)
Discussion started by: Legend986
1 Replies
Login or Register to Ask a Question