Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
google site



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Search this Thread
  #1  
Old 03-11-2010
Registered User
 

Join Date: May 2008
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Comma in regular expression

Hi,

I need to check if string contains comma...
Wrote like ~ m/\,/ but no luck...

Also taken comma in variable and checked with ~ m/($comma)/ but this also doesn't help....

Please advise
Sponsored Links
  #2  
Old 03-11-2010
murugaperumal's Avatar
Registered User
 

Join Date: Feb 2010
Location: Chennai
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts

Code:
my $var;
while(1)
{
$var=<>;
if($var=~/,/)
{
    print $var;
    last;
}
}

  #3  
Old 03-16-2010
Nila's Avatar
Registered User
 

Join Date: Feb 2010
Posts: 85
Thanks: 1
Thanked 0 Times in 0 Posts
Hi , darshakraut your method also should work if your string is valid.

Try this,

Code:
if($string=~m/,/)

  #4  
Old 03-16-2010
ungalnanban's Avatar
Registered User
 

Join Date: Feb 2010
Location: Chennai
Posts: 126
Thanks: 3
Thanked 8 Times in 5 Posts
Thumbs up

see the following regular expression.


Code:
if ( $val =~ m/[,]/)

[or]

Code:
if ( $val =~ m%,%)

you can change the delimiter instead of "/" like %, #, !, | etc.,



Last edited by ungalnanban; 03-16-2010 at 06:53 AM..
  #5  
Old 03-16-2010
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,545
Thanks: 2
Thanked 7 Times in 7 Posts
Wink

what about with a grep
if

Code:
sample="hello,fool"

then

Code:
echo "$sample" | grep -c ","

returns 1
while

Code:
sample1="not here"
echo "$sample1" | grep -c ","

returns 0

and the result could be put to a variable.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Regular Expression siba.s.nayak Shell Programming and Scripting 3 11-12-2009 05:48 PM
Regular Expression mradsus Shell Programming and Scripting 2 08-05-2008 04:29 AM
regular expression...help!! andy2000 UNIX for Dummies Questions & Answers 6 07-18-2007 05:10 PM
awk and regular expression maskot Shell Programming and Scripting 4 05-22-2007 07:22 AM
Regular Expression + Aritmetical Expression Z0mby Shell Programming and Scripting 2 05-21-2002 10:59 AM



All times are GMT -4. The time now is 09:20 PM.