Perl code to search for filenames that contain special characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl code to search for filenames that contain special characters
# 1  
Old 01-18-2006
Perl code to search for filenames that contain special characters

Hello,

I have a requirement to search a directory, which contains any number of other directories for file names that contain special characters.

directory structure

Code:
DIR__
     |__>DIR1
     |__>DIR2__
               |__>DIR2.1
               |__>DIR2.2
     |__>DIR3
..
     |__>DIRn

There could be any number of files within this directory DIR.
In shell I could use the following to return the list of file names

Code:
ls -R DIR | sed -e '/^$/d'

This returns both directory names as well as file names.

The characters that I need to search for are

Code:
\ / : * ? < > |

how can I interrogate the file name for these characters and report on it?

Thanks
Jerardfjay
# 2  
Old 01-18-2006
see whether this helps..

$ ls -lrt
total 56
drwxrwx--- 3 xxxx xxxxxx 4096 Jan 6 01:05 lib
-rw-rw---- 1 xxxx xxxxxx 5 Jan 16 18:52 tmp2
-rw-rw---- 1 xxxx xxxxxx 1417 Jan 17 18:29 logfile
-rw-rw---- 1 xxxx xxxxxx 9 Jan 18 13:40 tab_del.dat
-rwxrwx--x 1 xxxx xxxxxx 105 Jan 18 17:59 tmp1.sh
-rw-rw---- 1 xxxx xxxxxx 109 Jan 18 18:11 datafile
-rw-rw---- 1 xxxx xxxxxx 129 Jan 18 18:25 datafile1
-rw-rw---- 1 xxxx xxxxxx 0 Jan 18 19:01 mah*ab
-rw-rw---- 1 xxxx xxxxxx 0 Jan 18 19:33 amh\hai
-rwxrwx--x 1 xxxx xxxxxx 148 Jan 18 19:36 tmp1.pl
-rwxrwx--- 1 xxxx xxxxxx 195 Jan 18 19:44 tmp2.pl

This is actual command :

$ find . -name "*" -print | sed 's/\\/\\\\/g' | tmp2.pl | sed 's/\\\\/\\/g'
Patter found in ./mah*ab

Patter found in ./amh\hai


here is the tmp2.pl code :

#!/usr/bin/perl -w

while( $line=<STDIN> )
{
my $vl = qx(basename $line);

if ( $vl =~ /\*|\<|\>|\?|\||:|\\|\// )
{
print "Patter found in $line\n";
}
}

the sed commands before and after tmp2.pl is needed to handle "\" character, if you are sure that it won't be there, then you can take out the sed command on both the places...

but i don't think "/" can be found in the unix file name because this is used for folders...

Last edited by mahendramahendr; 01-18-2006 at 03:55 PM..
# 3  
Old 01-18-2006
Quote:
Originally Posted by mahendramahendr
but i don't think "/" can be found in the unix file name because this is used for folders...
Thanks.

I have tried your code yet. Is it possible to create a file name with a "/" using perl itself. Or if the file name was created in a windows system and ftp'd to a UNIX system using perl FTP. Please advise. Thanks

Jerardfjay
# 4  
Old 01-18-2006
I tried to create file in windows with "/" but it failed..
how ever the script i have posted above can handle the character "/" as well.
so you can go ahead and try the script....

let me know if there are any errors with the error message
# 5  
Old 01-18-2006
Quote:
Originally Posted by mahendramahendr
see whether this helps..

This is actual command :

$ find . -name "*" -print | sed 's/\\/\\\\/g' | tmp2.pl | sed 's/\\\\/\\/g'
Patter found in ./mah*ab

Patter found in ./amh\hai
Mahendramahendr,

How can I perform the recursive find within the perl program itself. Dont mean to be rude, however this is my requirement. Please advise. Thanks

Jerardfjay
# 6  
Old 01-18-2006
#!/usr/bin/perl -w

open(FLDR," find . -name '*' | sed 's/\\\\/\\\\\\\\/g' | ");
while( $line=<FLDR> )
{
my $vl = qx(basename $line);

if ( $vl =~ /\*|\<|\>|\?|\||:|\\|\// )
{
$line =~ s/\\\\/\\/g;
print "Patter found in $line";
}
}
close FLDR;

output :

$ tmp2.pl
Patter found in ./mah*ab
Patter found in ./amh\hai

is this ok ?

Last edited by mahendramahendr; 01-18-2006 at 04:51 PM..
# 7  
Old 01-18-2006
Quote:
Originally Posted by mahendramahendr
#!/usr/bin/perl -w

open(FLDR," find . -name '*' | sed 's/\\\\/\\\\\\\\/g' | ");
while( $line=<FLDR> )
{
my $vl = qx(basename $line);

if ( $vl =~ /\*|\<|\>|\?|\||:|\\|\// )
{
$line =~ s/\\\\/\\/g;
print "Patter found in $line";
}
}
close FLDR;

output :

$ tmp2.pl
Patter found in ./mah*ab
Patter found in ./amh\hai

is this ok ?
Perfect. Just what I wanted. Thanks for your time. while you were trying to get it to work I was trying myself and came up with this piece of code.

Code:
#! /usr/bin/perl
$dir="test_dir"
$out = `ls -R $dir | egrep -v ":" `;
foreach $fname (split /\n/, $out) {
      if ( $fname =~ /\*|\<|\>|\?|\||:|\\|\//) {
         print "File name { $fname } contains an invalid character in directory { $dir }\n";
         }
}

Contents of test_dir : ls -R test_dir
Code:
DIR1           DIR2          DIR3
DIR/DIR1:
test1

DIR/DIR2:
This \ might be an overkill

DIR/DIR3:
* this really does it

Executing test.pl I get the output

Code:
File name { This \ might be an overkill } contains an invalid character in directory { test_dir }
File name { * this really does it } contains an invalid character in directory { test_dir }

Appreciate your time.
Regards
Jerardfjay
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Search special characters in a file and replace with meaningful text messages like Hello

Search special characters in a file and replace with meaningful text messages like Hello (2 Replies)
Discussion started by: raka_rjit
2 Replies

2. Shell Programming and Scripting

Search avoiding special characters

Hi all, I have a list which I want to search in another file. I can do that using grep -f but the search is failing due to special characters, how do I solve this? One row in that list is amino-acid permease inda1 gb|EDU41782.1| amino-acid permease inda1 Input file to be searched... (2 Replies)
Discussion started by: gina.lizar
2 Replies

3. Shell Programming and Scripting

Perl: windows filenames escape characters

I have a perl find program that will find all files of window application stored on unix disks. Ofcourse these files contain all the weird characters windows allows, but on *nix pukes out all kinds of unwanted effects when processing these. Is their a utility that will escape all these... (3 Replies)
Discussion started by: karelb
3 Replies

4. Shell Programming and Scripting

awk search pattern with special characters passed from CL

I'm very new to awk and sed and I've been struggling with this for a while. I'm trying to search a file for a string with special characters and this string is a command line argument to a simple script. ./myscript "searchpattern" file #!/bin/sh awk "/$1/" $2 > dupelistfilter.txt sed... (6 Replies)
Discussion started by: cue
6 Replies

5. Shell Programming and Scripting

Replace special characters in multiple files - perl

I have 100 files, where i want to search a set of strings and make the replacement by other strings In the first case I want to include a parameter in the name of a file LOG_DCT = $ LOG_DIR/DCT_GERAL_"$DATAINI".log replace to : LOG_DCT = $ LOG_DIR / DCT_GERAL_ $ 1_ "$ DATAINI". log I did... (1 Reply)
Discussion started by: RMSoares
1 Replies

6. Shell Programming and Scripting

special characters handling in perl

Hi, Here is my piece of code-- sub per_user_qna_detail { for($index=0;$index<@records;$index++) { if($records =~ m/^(.*)\s*Morocco.*Entering\s*Module::authenticate/) { printf "INSIDE per_user_qna_detail on LINE NO $index\n"; $Time_Stamp = $1;... (0 Replies)
Discussion started by: namishtiwari
0 Replies

7. Shell Programming and Scripting

Want the substring of a string with special characters using perl

Hi, I am trying to separate the directory from the filename using the substring and it does not like it. $Complete_name="C:\ABCD\Reports_Split\090308.1630" I want $File_Name as 090308.1630 I tried using Substring function and it does not like slashes in the complete_name. Any... (5 Replies)
Discussion started by: sushma0907
5 Replies

8. Shell Programming and Scripting

remove special characters from text using PERL

Hi, I am stuck with a problem here. Suppose i have a variable which is assigned some string containing special charatcers. for eg: $a="abcdef^bbwk#kdbcd@"; I have to remove the special characters using Perl. The text is assigned to the variable implicitly. How to do it? (1 Reply)
Discussion started by: agarwal
1 Replies

9. Shell Programming and Scripting

Open filename with special characters in Perl

Hi All, I am facing a weird problem. I have got a directory structure copied from windows to Linux. Some of the folders are named like /gfs/data/Dow Jones $5/ DJ FCBOT_O_tick_1998.zip /gfs/data/Dow Jones $5/ DJ FCBOT_O_tick_2000.CSV /gfs/data/Dow Jones... (6 Replies)
Discussion started by: mitrashatru
6 Replies

10. UNIX for Dummies Questions & Answers

search special characters in a file

Hello I am new to shell scripting and can anyone tell me how to check if there are any special characters in a file. Can i use grep ? thanks susie (2 Replies)
Discussion started by: cramya80
2 Replies
Login or Register to Ask a Question