LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   regular exp. (https://www.linuxquestions.org/questions/programming-9/regular-exp-600478/)

monu 11-17-2007 06:37 PM

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

PAix 11-17-2007 07:00 PM

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

monu 11-17-2007 07:41 PM

Quote:

Originally Posted by PAix (Post 2962502)
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

monu 11-17-2007 07:48 PM

here is my code:

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

can anyone can help this?

chrism01 11-17-2007 08:04 PM

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

PAix 11-17-2007 08:57 PM

I sidelined myself and assumed he was wanting a match of linux LINUX etc with square brackets - it must be getting late.

chrism01 11-17-2007 09:14 PM

In the UK def late. Lunchtime here :)

monu 11-18-2007 01:04 AM

Quote:

Originally Posted by chrism01 (Post 2962561)
why the square brackets?
in=`echo $input|grep -i linux`
if [[ $? -eq 0 ]]
then
echo "match"
fi

good work! thanks for your help!

PAix 11-18-2007 08:35 AM

Chrism01,
Def late= > 3.30am UK local time, well after supper and too long before breakfast - not a bad time to consider sleeping. :) PAix

monu 11-18-2007 04:20 PM

Quote:

Originally Posted by chrism01 (Post 2962561)
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

chrism01 11-19-2007 01:19 AM

$? 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?

Disillusionist 11-19-2007 01:53 AM

Quote:

Originally Posted by monu (Post 2963411)
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"


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