LinuxQuestions.org
Help answer threads with 0 replies.
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 05-17-2013, 04:04 AM   #1
ramsavi
LQ Newbie
 
Registered: May 2013
Posts: 16

Rep: Reputation: Disabled
problem in pattern match


Hi all,

i am using this code , but having some problem,

Code:
pattern=File_[0-9]*.csv
pattern1=File_[0-9]*_[0-9]*.csv
and then i am comparing the patterns like this

Code:
case ${arr[$i]} in
  $pattern) echo "pattern %N% match" ;;
  esac
case ${arr[$i]} in
  $pattern1) echo "pattern %N%_%N%  match" ;;
  esac

but if name of my file is File_23_23.csv
then it is returning

pattern %N% match
pattern %N%_%N% match

but the desired result should be only pattern %N%_%N% match only.

can any one please help me.
 
Old 05-17-2013, 05:45 AM   #2
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
Try reversing the order of the tests; the first pattern is a subset of the second.

Actually, you also should use this format
Code:
# set -xv
#f=File_23_23.csv
f=File_23.csv

pattern='File_[0-9]*.csv'
pattern1='File_[0-9]*_[0-9]*.csv'

case $f in
  $pattern1) echo "pattern %N%_%N%  match" ;;
  $pattern) echo "pattern %N% match" ;;
esac
Try changing which $f value you test to see what happens or even enable the 'set -xv' cmd for debug output

You need to be careful using '*' in a regex or glob.
 
Old 05-17-2013, 05:47 AM   #3
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
Case uses globbing and no regex you could try
Code:
pattern=File_*([0-9]).csv
 
Old 05-17-2013, 08:07 AM   #4
ramsavi
LQ Newbie
 
Registered: May 2013
Posts: 16

Original Poster
Rep: Reputation: Disabled
i used the code
but it is not working.
 
Old 05-17-2013, 08:17 AM   #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
Quote:
but it is not working.
tells us nothing.

Show your code and what happened when you ran it.
Did you even try the debug I told you about??
 
Old 05-17-2013, 08:54 AM   #6
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
For this to work you have to enable extended globbing, assuming you use bash.
Code:
shopt -s extglob
pattern=File_*([0-9]).csv
 
1 members found this post helpful.
Old 05-17-2013, 11:44 AM   #7
ramsavi
LQ Newbie
 
Registered: May 2013
Posts: 16

Original Poster
Rep: Reputation: Disabled
thanks a lot its working..
 
Old 05-18-2013, 09:32 AM   #8
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
And I suggest placing your variable around pairs of double quotes for safety.
Code:
case "${arr[$i]}" in
...
 
1 members found this post helpful.
  


Reply



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
[SOLVED] rm files that do not match a given pattern elsheikhmh Linux - Newbie 16 02-08-2014 09:52 PM
how to match for a particular pattern ramsavi Solaris / OpenSolaris 3 05-15-2013 09:46 PM
Match a pattern in linux knoweledgeseeker Linux - General 5 08-19-2012 07:10 AM
[SOLVED] Adding (not replacing) a pattern match with a similar pattern? b-bri Linux - Newbie 2 08-31-2009 12:36 AM
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM

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

All times are GMT -5. The time now is 11:02 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
Open Source Consulting | Domain Registration