perl linux file name validation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl linux file name validation
# 8  
Old 07-06-2009
Quote:
Originally Posted by jimmy_y
Thanks Kevin,

But when i use:
#!/usr/bin/perl
$a = "a";
#if ($a =~ s/[A-Za-z0-9_-.]//g) {
if ($a =~ s/[A-Za-z0-9_\-.]//g) {
print "invalid file name\n";
} else {
print "valid file name\n";
}

i got
invalid file name

Smilie
Of course that's what you get. Your script is saying:

Code:
if (we substitute one of these characters for nothing) {
   then the filename is invalid
{
else {
   it is valid
}

Since $a has an 'a' character in it, the condition is true because the regexp substitutes the 'a' for nothing then enters the block and tells you the filename is invalid. I think what you are trying to do is validate that a filename only has valid characters which would be done like this:

Code:
#!/usr/bin/perl
$a = "ABCtest-12_3.txt";
if ($a =~ /^[\w.-]+$/) {
        print "valid file name\n";
} else {
        print "invalid file name\n";
}

Although that is not very good validation for a filename since it will pass a single dot or single dash. But that is the basics.

---------- Post updated at 01:49 PM ---------- Previous update was at 01:46 PM ----------

Quote:
Originally Posted by pludi
First, please use [code][/code] tags for sources, code/output listings, ...
Second, [A-Za-z0-9_] has it's own character class in Perl, \w
Third, if you want to match a literal dot, escape it, otherwise it'll match any character
Fourth, if you want to match something, don't run substitution on it.
Fifth, use appropriate logic. In your example the regex matches, but your logic says it's invalid.

This will work:
Code:
#!/usr/bin/perl
$a = "a";
if ( $a =~ /[-\w\.]/g ) {
    print "valid file name\n";
}
else {
    print "invalid file name\n";
}


The dot in a character class is just a dot, it is not a wild card match and does not need escaping inside a character class. Test it for yourself.
# 9  
Old 07-07-2009
Quote:
Originally Posted by KevinADC
Of course that's what you get. Your script is saying:

Code:
if (we substitute one of these characters for nothing) {
   then the filename is invalid
{
else {
   it is valid
}

Since $a has an 'a' character in it, the condition is true because the regexp substitutes the 'a' for nothing then enters the block and tells you the filename is invalid. I think what you are trying to do is validate that a filename only has valid characters which would be done like this:

Code:
#!/usr/bin/perl
$a = "ABCtest-12_3.txt";
if ($a =~ /^[\w.-]+$/) {
        print "valid file name\n";
} else {
        print "invalid file name\n";
}

Although that is not very good validation for a filename since it will pass a single dot or single dash. But that is the basics.

---------- Post updated at 01:49 PM ---------- Previous update was at 01:46 PM ----------




The dot in a character class is just a dot, it is not a wild card match and does not need escaping inside a character class. Test it for yourself.
Thanks, i go to verify.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux xmllint schema validation fails but error code 0

Command line xmllint --schema validation fails but $? returns 0 myinput.xml: <myinput><header>mytestvalue</header></myinput>myschema.xsd <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="myinput" type="xsd:string"/> </xsd:schema>Command: $xmllint --schema... (4 Replies)
Discussion started by: mknag
4 Replies

2. Shell Programming and Scripting

Get the File name of perl executable in Linux

Hi All, I just want to know how to get the executable name of the perl script as i know "$0" will give me the script name but i want to know the executable name which i got it from the script using pp command. Regards Raj (1 Reply)
Discussion started by: kar_333
1 Replies

3. Shell Programming and Scripting

File validation

Hi there, As a part of file validation, I needed to check for delimiter count in the file. My aim is to find, how many records have failed to have predefined numbers of delimiters in the file. My code looks like below i=`awk -F '|' 'NF != 2 {print NR, $0} ' ${pinb_fldr}/${pfile}DAT |... (3 Replies)
Discussion started by: anandapani
3 Replies

4. Shell Programming and Scripting

Validation of linux scripts

Hi, I have a script which invokes another script inside. Is there any way I can validate the script for syntax errors as well as checking if the files mentioned in the commands exist without executing it? Below is an example Example. ======================= Script1 #ksh echo... (3 Replies)
Discussion started by: krish000
3 Replies

5. Shell Programming and Scripting

perl: How to improve with minimal validation of its input??

The Code: #!/usr/bin/perl use strict; use warnings; print "Please enter numbers, separated by commas: "; my $data=<STDIN>; chomp $data; my @dataset=split(/,/, $data); my $sum = 0; foreach my $num (@dataset) { $sum += $num; } my $total_nums = scalar(@dataset); my $mean =... (1 Reply)
Discussion started by: 300zxmuro
1 Replies

6. Shell Programming and Scripting

File validation

Hello, File contains 1,3 and 5 are required columns. it's working use this command. awk -F\| '$1 && $3 && $5' test1.txt > test2.txt How can use this unix programming.while using runnign this scirpt,it's raising the error. awk: ^ syntax error #!/usr/bin/ksh `awk -F\| '$1 &&... (3 Replies)
Discussion started by: bammidi
3 Replies

7. Shell Programming and Scripting

File Name Validation

Hi All, I am trying to validate the file name against the naming convention mentioned in configuration file. In the configuration file, the file name convention is mentioned as: Myfile_SQ<NN>_<NN>_YYYYMMDD_HHMMSS.xml The actual file received is Myfile_SQ10_30_20110423_073002.xml How do... (1 Reply)
Discussion started by: angshuman
1 Replies

8. Shell Programming and Scripting

Perl script :- Phone number validation

Hi All, I am doing a perl script validation for Phone numbers. The normal phone number format is 01-32145. I need to do two validations for the phone number 1) A valid phone number can have at least two digits as prefix and at least five digits as postfix. e.g. 01-01011 2) A... (5 Replies)
Discussion started by: subin_bala
5 Replies

9. UNIX for Dummies Questions & Answers

File Validation

Hi All, I am new to UNIX scripting. I need to validate a file. I need to check whether the file has a header , detail records and footer. If all the file is good I need to create a status file with the status 'Y' else 'N'. I have pasted the an example of the file below: ... (5 Replies)
Discussion started by: kumar66
5 Replies

10. Shell Programming and Scripting

form validation with perl

Hey guys, I'm just messing around with a perl webpage. The idea is to make a simple validation form that will later insert a record into my DVD database. it's all very basic at the moment, and I worked up my script from the form validation example I found on this website:... (5 Replies)
Discussion started by: LNC
5 Replies
Login or Register to Ask a Question