LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 11-17-2007, 06:37 PM   #1
monu
LQ Newbie
 
Registered: Jun 2007
Posts: 17

Rep: Reputation: 0
regular exp.


Hi,

I am working on script which can ignore Case sensitivity. suppose if user enter Linux or LINUX, scripr should accept both ways.

I use:
grep -i "[linux]"
grep -i "[LINUX]"


its not working. My script is not accepting Linux. its not accepting small letters linux.

can anyone help me?


thanks
 
Old 11-17-2007, 07:00 PM   #2
PAix
Member
 
Registered: Jul 2007
Location: United Kingdom, W Mids
Distribution: SUSE 11.0 as of Nov 2008
Posts: 195

Rep: Reputation: 40
Code:
 grep -i "\[linux\]" filenametosearch
Your square brackets need to be escaped and then I am sure that it will work. The filename/s to search go after the pattern to search for of course. .

It will happily find [linux], [LINUX], [LiNuX] etc.
PAix

Last edited by PAix; 11-17-2007 at 07:01 PM.
 
Old 11-17-2007, 07:41 PM   #3
monu
LQ Newbie
 
Registered: Jun 2007
Posts: 17

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by PAix View Post
Code:
 grep -i "\[linux\]" filenametosearch
Your square brackets need to be escaped and then I am sure that it will work. The filename/s to search go after the pattern to search for of course. .

It will happily find [linux], [LINUX], [LiNuX] etc.
PAix
nope. not working
thanks anyways
 
Old 11-17-2007, 07:48 PM   #4
monu
LQ Newbie
 
Registered: Jun 2007
Posts: 17

Original Poster
Rep: Reputation: 0
here is my code:

li=`echo $userinput | grep -i "\[linux\]"`

can anyone can help this?
 
Old 11-17-2007, 08:04 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
why the square brackets?
in=`echo $input|grep -i linux`
if [[ $? -eq 0 ]]
then
echo "match"
fi
 
Old 11-17-2007, 08:57 PM   #6
PAix
Member
 
Registered: Jul 2007
Location: United Kingdom, W Mids
Distribution: SUSE 11.0 as of Nov 2008
Posts: 195

Rep: Reputation: 40
I sidelined myself and assumed he was wanting a match of linux LINUX etc with square brackets - it must be getting late.
 
Old 11-17-2007, 09:14 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
In the UK def late. Lunchtime here
 
Old 11-18-2007, 01:04 AM   #8
monu
LQ Newbie
 
Registered: Jun 2007
Posts: 17

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by chrism01 View Post
why the square brackets?
in=`echo $input|grep -i linux`
if [[ $? -eq 0 ]]
then
echo "match"
fi
good work! thanks for your help!
 
Old 11-18-2007, 08:35 AM   #9
PAix
Member
 
Registered: Jul 2007
Location: United Kingdom, W Mids
Distribution: SUSE 11.0 as of Nov 2008
Posts: 195

Rep: Reputation: 40
Chrism01,
Def late= > 3.30am UK local time, well after supper and too long before breakfast - not a bad time to consider sleeping. PAix
 
Old 11-18-2007, 04:20 PM   #10
monu
LQ Newbie
 
Registered: Jun 2007
Posts: 17

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by chrism01 View Post
why the square brackets?
in=`echo $input|grep -i linux`
if [[ $? -eq 0 ]]
then
echo "match"
fi
Hi

what happen if i want to check two arguments?

in=`echo $input|grep -i linux`
in=`echo $input|grep -i fedora`
if [[ $? -eq 0 ]]
then
echo "match"
fi


its not working at all.

thanks
 
Old 11-19-2007, 01:19 AM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
$? is the status of the last run cmd, so you'd have to check each grep separately.
Perhaps you could expand on why you are doing this?
 
Old 11-19-2007, 01:53 AM   #12
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Quote:
Originally Posted by monu View Post
Hi

what happen if i want to check two arguments?

in=`echo $input|grep -i linux`
in=`echo $input|grep -i fedora`
if [[ $? -eq 0 ]]
then
echo "match"
fi


its not working at all.

thanks
If you want to check that $input includes both "linux" and "fedora" :
Code:
echo $input|grep -i linux|grep -i fedora
If you want to check that $input includes either "linux" or "fedora" :
Code:
echo $input|egrep -i "linux|fedora"
 
  


Reply

Tags
grep



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
python reg exp help Bopi Programming 1 12-03-2006 02:29 AM
prob with reg exp rajatgarg Programming 3 05-28-2004 09:21 AM
exp about FreeBSD installation lifetaster Linux - Newbie 2 02-18-2004 06:43 PM
First Linux Exp Jkool Linux - General 6 01-21-2004 09:16 PM
Reg Exp ugenn Programming 4 09-19-2002 12:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:07 AM.

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
Open Source Consulting | Domain Registration