1753259 Members
6001 Online
108792 Solutions
New Discussion

Re: Check for format

 
SOLVED
Go to solution
Pavitra
Occasional Advisor

Check for format

can anybody help me in this??
i want to check whether the filename is like
XXXNN.XXNNNNNN.XXX
where X-> alphabets,
N-> numbers
4 REPLIES 4
Dennis Handly
Acclaimed Contributor
Solution

Re: Check for format

You can use pattern matching:
[A-Za-z_][A-Za-z_][A-Za-z_][0-9][0-9].[A-Za-z_][A-Za-z_][0-9][0-9][0-9][0-9][0-9][0-9].[A-Za-z_][A-Za-z_][A-Za-z_]

for i in $*; do
    echo $i | grep '[A-Za-z_][A-Za-z_][A-Za-z_][0-9][0-9]\.[A-Za-z_][A-Za-z_][0-9][0-9][0-9][0-9][0-9][0-9]\.[A-Za-z_][A-Za-z_][A-Za-z_]'
done

This will echo the name if it matches.
(The grep is one long pattern.)

Perhaps this is easier to understand:
A="[A-Za-z_]" # includes "_" if you want
N="[0-9]"
for i in $*; do
    echo $i | grep "$A$A$A$N$N\.$A$A$N$N$N$N$N$N\.$A$A$A"
done

Dennis Handly
Acclaimed Contributor

Re: Check for format

Or you could use:
A="[[:alpha:]]" # no "_" this time
N="[[:digit:]]"
for i in $*; do
    echo $i | grep "$A\{3\}$N\{2\}\.$A\{2\}$N\{6\}\.$A\{3\}"
done

James R. Ferguson
Acclaimed Contributor

Re: Check for format

Hi Pavitra:

You could do pass your file name to this Perl snippet to test it. If the name meets your criteria, "ok" is printed, otherwise nothing:

# echo abc12.de123456.fgh | perl -nle 'print "ok" if /^\pL{3}\d\d\.\pL\pL\d{6}\.\pL{3}$/'

The \pL represents one alphabetic character. The \d stands for one digit. The {n} notation specifies the number of occurances. The caret anchors the match to the beginning of the string, and the dollar-sign to the end.

Regards!

...JRF...
Arturo Galbiati
Esteemed Contributor

Re: Check for format

Hi,
ll [A-Za-z_][A-Za-z_][A-Za-z_][0-9][0-9].[A-Za-z_][A-Za-z_][0-9][0-9][0-9][0-9][0-9][0-9].[A-Za-z_][A-Za-z_][A-Za-z_]

HTH,
Art