LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-22-2005, 02:04 AM   #1
smart_sagittari
LQ Newbie
 
Registered: Apr 2005
Posts: 26

Rep: Reputation: 16
Using regular expression in filename



I have a script which gets as input a filename and does a grep on that. Now the problem is something like this:

The files I need to look in for the pattern are named as follows:

out, out.20050421.1234, out.20050421.2134, out.20050420.0012

My program gets as input the filename upto before the last . [DOT], like either, out [for the first file] or out.20050421 for the second and third files.

Now, in the second case [file name input out.20050421], I directly can use grep pattern out.20050421.[0-9]* to get results from the second and third files stated above [desired output]

However, if I get the filename as out, doing a out[0-9]* doesnt work [I dont know why]

Can someone give me a solution to this[assuming that I have not confused people enough]!!!

Last edited by smart_sagittari; 04-22-2005 at 02:31 AM.
 
Old 04-23-2005, 10:56 AM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,337

Rep: Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548
"However, if I get the filename as out, doing a out[0-9]* doesnt work [I dont know why]"
Are you mising a period?
Shouldn't that be out.[0-9]*

------------------------------
Steve Stites
 
Old 04-24-2005, 10:02 PM   #3
smart_sagittari
LQ Newbie
 
Registered: Apr 2005
Posts: 26

Original Poster
Rep: Reputation: 16
Thats the problem. The base file doesnt have a . [DOT]. How can I go abt this. Is there some way I can include a . inside the brackets [so as to consider it an optional dot]?
 
Old 04-25-2005, 01:02 AM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
I'm not sure your wildcard is doing what you hope it does (assuming this is a shell script and not perl).

In the shell, using "[0-9]*" says there must be a single character between 0 and 9 (inclusive), followed by any number of other characters. In other words, "out[0-9]*" would match:
out0
out1
out2a
out3abc
out456z
...

It's not the same behavior as you'd get from a traditional regular expression.

You might want to consider using the '?' wildcard, but I don't know if that would really solve the problem.
 
Old 04-25-2005, 01:09 AM   #5
smart_sagittari
LQ Newbie
 
Registered: Apr 2005
Posts: 26

Original Poster
Rep: Reputation: 16
Thanks for the suggestion. I shall certainly have a look into it.
 
Old 04-25-2005, 01:20 AM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
If you're using Bash, you'll be interested to read the secion regarding "pathname expansion". Specifically, the pattern matching sub-section. It explains how the wildcards work. In addition to the basic wildcards (*, ?, [] ), there are some expressions that have similar effects as traditional regular expressions. For instance:
Code:
       If the extglob shell option is enabled using the shopt builtin, several
       extended pattern matching operators are recognized.  In  the  following
       description, a pattern-list is a list of one or more patterns separated
       by a |.  Composite patterns may be formed using one or more of the fol-
       lowing sub-patterns:

              ?(pattern-list)
                     Matches zero or one occurrence of the given patterns
              *(pattern-list)
                     Matches zero or more occurrences of the given patterns
              +(pattern-list)
                     Matches one or more occurrences of the given patterns
              @(pattern-list)
                     Matches exactly one of the given patterns
              !(pattern-list)
                     Matches anything except one of the given patterns
That's from my man page (bash version 3).

Last edited by Dark_Helmet; 04-25-2005 at 01:22 AM.
 
  


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
Find/grep command to find matching files, print filename, then print matching content stefanlasiewski Programming 9 06-30-2016 05:30 PM
Print-to-file print driver to print PDF Bill Fox Linux - General 3 05-02-2006 04:15 PM
force grep to keep it's place in file for next iteration? jeffreybluml Programming 7 05-13-2005 10:16 AM
Setting up a print to file print driver Bill Fox Linux - General 0 02-26-2005 01:22 PM
Force Mozilla to Print to CUPS? simsjr Linux - Hardware 8 12-01-2004 12:22 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:15 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