LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 09-18-2012, 10:37 PM   #1
the_gripmaster
Member
 
Registered: Jul 2004
Location: VIC, Australia
Distribution: RHEL, CentOS, Ubuntu Server, Ubuntu
Posts: 362

Rep: Reputation: 38
Question Regex help


This is just a simple script which echoes to the screen whatever the user types and quits when the user types quit or QUIT.

Code:
while read LINE; do 
  if [[ "$LINE" =~ quit|QUIT ]]; then 
    break
  fi 
  echo $LINE
done
How do I add case insensitivity to the quit word so that typing in quit, QuIt, Quit, or any other possibility matches the if statement?
 
Old 09-18-2012, 11:21 PM   #2
tc_
LQ Newbie
 
Registered: Sep 2010
Location: Germany
Distribution: Slackware
Posts: 28

Rep: Reputation: 30
Try
Code:
if [[ $(echo $LINE | tr [:lower:] [:upper:]) = "QUIT" ]]; then
    break
fi
See also the man page of 'tr'.
 
Old 09-18-2012, 11:35 PM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 640

Rep: Reputation: 375Reputation: 375Reputation: 375Reputation: 375
Hi.

You can also use
Code:
shopt -s nocasematch
to enable case-insensitive matching:

man bash:
Quote:
nocasematch
If set, bash matches patterns in a case-insensitive fashion when performing matching while executing case or [[ conditional commands.
 
Old 09-19-2012, 11:53 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Or if bash is version 4 or greater:
Code:
[[ ${LINE^^} == QUIT ]] && break
 
  


Reply

Tags
bash, regex


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
New to regex, need help aiman Programming 11 08-26-2011 07:28 AM
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 02:10 AM
Perl to find regex and print following 5 lines after regex casperdaghost Linux - Newbie 3 08-29-2010 09:08 PM
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 06:10 AM
Need a regex, I suck at regex's d3funct Programming 4 02-25-2002 09:28 PM


All times are GMT -5. The time now is 09:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration