[K12OSN] OT: check case and format of filename

Rob Owens rob.owens at biochemfluidics.com
Tue Sep 30 12:02:13 UTC 2008


Nils Breunese wrote:
> Rob Owens wrote:
> 
>> Nils Breunese wrote:
>>> Rob Owens wrote:
>>>
>>>> Since list has been kinda slow lately, I'll test your patience with one
>>>> more scripting question.  You guys have really helped me learn a lot in
>>>> the past couple of days.  I've been looking into bash parameter
>>>> expansion, awk, and sed, but I'm not really sure I'm going in the right
>>>> direction.
>>>>
>>>> I need to check a filename for a particular "format" or "convention".
>>>> File names should look like this:
>>>>
>>>> AB12D89-S123--revA
>>>> AB12D89-S123--revA--page1
>>>>
>>>> In other words:
>>>>
>>>> 1)  They should be all uppercase and numeric before the '--'
> 
> There's also a dash before the '--'. That not uppercase or numeric... I
> added dash to the allowed characters before '--' in the regular
> expression below.
> 
>>>> 2)  They must contain '--revX', where X is any uppercase letter, and
>>>> 'rev' must be lowercase.
>>>> 3)  There is an optional --page# at the end, where # is any number, and
>>>> 'page' must be lowercase.
>>>>
>>>> I can work out the logic of testing for compliance with my rules, but
>>>> I'm not sure how to identify the case of each letter, nor how to split
>>>> the filename up using '--' as the divider.
>>>>
>>>> Thanks for any advice you might have.  By the way, this is to be
>>>> part of
>>>> a bash script (which is to be run on my K12LTSP box).
>>>
>>> Do you need a function that returns true or false depending on whether a
>>> string (filename) complies with the format or do you need something that
>>> returns all filenames in a directory that do or do not comply or
>>> something? Did you already figure out that you're probably looking at
>>> using regular expressions? http://www.regular-expressions.info/ has a
>>> lot of info on regular expressions.
>>>
>> Yes, I've been learning about regular expressions.  I appreciate the
>> link.  More stuff to read!
>>
>> I need to check one particular string for compliance with the format.  I
>> don't need to correct the format, I just need to inform the user that it
>> is incorrect.
> 
> Without going to Python or Perl or something you could try this:
> 
>     echo $filename | grep "^[A-Z0-9-]\+--rev[A-Z]\(--page[0-9]\+\)\?$"
> 
> and check the return value ($?, 0 for succes).
> 
Thanks, this worked perfectly.

I really appreciate everybody's help.  I also appreciate the advice to
learn Perl.  It's now officially on my list of things to do, as well as
reading up more on regular expressions.

-Rob
********************************************************

The information transmitted is intended only for the person or entity to
which it is addressed and may contain confidential and/or privileged
material. If you are not the addressee, any disclosure, reproduction,
copying, distribution, or other dissemination or use of this transmission in
error please notify the sender immediately and then delete this e-mail.
E-mail transmission cannot be guaranteed to be secure or error free as
information could be intercepted, corrupted lost, destroyed, arrive late or
incomplete, or contain viruses.
The sender therefore does not accept liability for any errors or omissions
in the contents of this message which arise as a result of e-mail
transmission. If verification is required please request a hard copy
version.

********************************************************




More information about the K12OSN mailing list