perl linux file name validation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl linux file name validation
# 1  
Old 07-06-2009
perl linux file name validation

Hi Everyone,

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

but the output is:
Invalid [] range "_-." in regex; marked by <-- HERE in m/[A-Za-z0-9_-. <-- HERE ]/ at ./a.pl line 5.

the linux file name should be A-Z, a-z, underscore, dash, dot.

Smilie Please advice where i made the mistake. Thanks
# 2  
Old 07-06-2009
just escape the dash:

Code:
if ($a =~ s/[A-Za-z0-9_\-.]//g) {

you could write it like this too:

Code:
if ($a =~ s/[\w.-]//g) {

in which case the dash does not need to be escaped because its on the end and \w is the same as a-zA-Z0-9_

The dot never needs to be escaped in a character class no matter where it is positioned but a few characters do, like a dash.
# 3  
Old 07-06-2009
Quote:
Originally Posted by KevinADC
just escape the dash:

Code:
if ($a =~ s/[A-Za-z0-9_\-.]//g) {

you could write it like this too:

Code:
if ($a =~ s/[\w.-]//g) {

in which case the dash does not need to be escaped because its on the end and \w is the same as a-zA-Z0-9_

The dot never needs to be escaped in a character class no matter where it is positioned but a few characters do, like a dash.
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
# 4  
Old 07-06-2009
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";
}

# 5  
Old 07-06-2009
Lightbulb

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";
}

Hi pludi, i tried if my filename $a = "a>";it says valid file name also, but this should be invalid. Smilie
# 6  
Old 07-06-2009
Whoops, my bad. Change
Code:
/[-\w\.]/g

to
Code:
/^[-\w\.]+$/

This will check that the whole string, and fail if there's a non-matching character.
# 7  
Old 07-06-2009
Quote:
Originally Posted by pludi
whoops, my bad. Change
Code:
/[-\w\.]/g

to
Code:
/^[-\w\.]+$/

this will check that the whole string, and fail if there's a non-matching character.
Thanks Smilie works perfect!
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