LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Force grep NOT to print file names (https://www.linuxquestions.org/questions/linux-newbie-8/force-grep-not-to-print-file-names-315953/)

smart_sagittari 04-22-2005 02:04 AM

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]!!!

jailbait 04-23-2005 10:56 AM

"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

smart_sagittari 04-24-2005 10:02 PM

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

Dark_Helmet 04-25-2005 01:02 AM

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.

smart_sagittari 04-25-2005 01:09 AM

Thanks for the suggestion. I shall certainly have a look into it.

Dark_Helmet 04-25-2005 01:20 AM

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).


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