The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Open filename with special characters in Perl mitrashatru Shell Programming and Scripting 6 02-14-2008 10:14 PM
Display special characters BCarlson Shell Programming and Scripting 2 10-06-2006 09:59 AM
search special characters in a file cramya80 UNIX for Dummies Questions & Answers 2 05-13-2005 12:08 PM
special characters nawnaw UNIX for Dummies Questions & Answers 2 05-18-2004 03:17 PM
awk/sed with special characters apalex Shell Programming and Scripting 5 05-06-2002 04:40 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-18-2006
jerardfjay jerardfjay is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 146
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 (permalink)  
Old 01-18-2006
mahendramahendr mahendramahendr is offline Forum Advisor  
Registered User
  
 

Join Date: Dec 2005
Location: London
Posts: 222
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 (permalink)  
Old 01-18-2006
jerardfjay jerardfjay is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 146
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 (permalink)  
Old 01-18-2006
mahendramahendr mahendramahendr is offline Forum Advisor  
Registered User
  
 

Join Date: Dec 2005
Location: London
Posts: 222
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 (permalink)  
Old 01-18-2006
jerardfjay jerardfjay is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 146
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 (permalink)  
Old 01-18-2006
mahendramahendr mahendramahendr is offline Forum Advisor  
Registered User
  
 

Join Date: Dec 2005
Location: London
Posts: 222
#!/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 (permalink)  
Old 01-18-2006
jerardfjay jerardfjay is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 146
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:34 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0