perl script to list filenames that do not contain given string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl script to list filenames that do not contain given string
# 15  
Old 04-22-2008
Hi era,

I checked your script in Linux (ubuntu - x86 m/c). It is still giving the same error what I have got in Cygwin, i.e.,

could not open ./ngilogger-1.0/src/ngilogger.txt: No such file or directory
./ngilogger-1.0/src/ngilogger.txt
could not open ./showimage-1.0/showimage.txt: No such file or directory
./showimage-1.0/showimage.txt

if we are running it from the top level directory. I don't have any Unix machine to check with either Smilie

Last edited by royalibrahim; 04-22-2008 at 07:11 AM..
# 16  
Old 04-22-2008
The script era posted has to be run from the directory you want the search to start in. I think he made that clear. If the start directory is not the same then feed the script a start directoy. See File::Find for details. If the script is in the start directory it looks to me like it should work.
# 17  
Old 04-22-2008
Well in any event, it "finds" those files because otherwise it would not be trying to open them.

Having Perl modules is good precisely because reinventing the wheel is not much fun, but here goes; a quick and dirty ugly half-assed broken replacement for File::Find.

/me secretly imagines you will have the same No such file or directory errors with this too, but at least it's simple enough that you can try to debug it yourself.

Code:
#!/usr/bin/perl

use strict;
use warnings;

die "usage: $0 dirs ...\n" unless (@ARGV);

while (@ARGV)
{
    curse(shift @ARGV);
}

sub curse
{
    my ($arg) = @_;

    if (-d $arg)
    {
	local *D;
	unless (opendir (D, "$arg"))
	{
	    warn "$0: could not open directory $arg: $!\n";
	    return 0;
	}
	while (my $dir = readdir D)
	{
	    next if ($dir eq "." || $dir eq "..");
	    curse("$arg/$dir");
	}
	closedir D;
	return 0;
    }

    elsif (-f _)
    {
	local *F;
	unless (open (F, $arg))
	{
	    warn "$0: could not open $arg: $!\n";
	    return 0;
	}
	my $matches = grep { /ALL -Tcb/ } <F>;
	close F;
	print "$arg\n" unless $matches;
	return ! $matches;
    }

    # else
    return 0;
}

# 18  
Old 04-22-2008
Try changing this line in the original code:

open (F, $File::Find::name) || warn "could not open $File::Find::name: $!\n";

to:

open (F, $_) || warn "could not open $File::Find::name: $!\n";

File::Find does a chdir by default into the current directory so using $_ should hopefully solve this problem
# 19  
Old 04-22-2008
KevinADC: good catch! I spotted the difference between $_ and $File::Find::name in the docs, but interpreted it precisely the other way around (didn't notice there would be a chdir)! And I obviously didn't test it in a deep-enough directory hierarchy, duh.
# 20  
Old 04-22-2008
Quote:
Originally Posted by KevinADC
Try changing this line in the original code:

open (F, $File::Find::name) || warn "could not open $File::Find::name: $!\n";

to:

open (F, $_) || warn "could not open $File::Find::name: $!\n";

File::Find does a chdir by default into the current directory so using $_ should hopefully solve this problem
Yes, KevinADC you are right. Your solution works perfectly without errors. Thanks a lot to you and to Era for the prompt responses/suggestions. Indeed I owe to you a lot.
# 21  
Old 04-22-2008
Actually the warning should also have $_, of course.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove string perl with first or last word is in a list

Hello, I try to delete all strings if their first or last word is one of this list of words : "the", "i", "in", "there", "this", "with", "on", "we", "that", "of" For example if i have this string in an input file "with me" this string will be removed, Example: input "the european... (2 Replies)
Discussion started by: cyrine
2 Replies

2. Shell Programming and Scripting

Perl script to filter the string

Hi folks, I have a log file with the lines in the below format. Jul 1 23:00:51 10.212.3.251 SS: %SYS-7-CLI_SCHEDULE: some error occured I want to split the line based on the " %SYS-7-CLI_SCHEDULE: " value. The criteria is the should store the word that starts with % i.e., ... (1 Reply)
Discussion started by: scriptscript
1 Replies

3. Shell Programming and Scripting

How to use perl to generate files with correct filenames?

Hi, I'm trying to use perl to generate files based on sections in a large textfile. This will create one file per section that starts with " ABC_": perl -n -e '/^ABC_/ and open FH, ">output_".$n++; print FH;' largefile.txt However, the output filenames will be on the form output_nn. This... (2 Replies)
Discussion started by: Yagi Uda
2 Replies

4. Shell Programming and Scripting

[Bash/Makefile] Concatenated string of filenames?

How would I go about storing a list of files in a sub-directory into a variable for argument passing? (2 Replies)
Discussion started by: ChazZeromus
2 Replies

5. UNIX for Dummies Questions & Answers

Find the list of filenames that have the string 31 at 4th and 5th position

Hi, Can anyone let me know the command to know the list of filenames that have string 31 in their 4th and 5th positions inside the file: grep -l "31" main*.txt The above grep lists all the files which have 31 at any position but I want filenames having 31 at position 4 and position 5. (8 Replies)
Discussion started by: okkadu
8 Replies

6. Shell Programming and Scripting

Perl script - Help me extracting a string

I have input like this : TNS Ping Utility for Linux: Version 11.2.0.1.0 - Production on 07-FEB-2012 04:19:45 Copyright (c) 1997, 2009, Oracle. All rights reserved. Used parameter files: /t3local_apps/apps/oracle/product/11.2.0/network/admin/sqlnet.ora Used TNSNAMES adapter to resolve... (3 Replies)
Discussion started by: dnam9917
3 Replies

7. Shell Programming and Scripting

List of filenames where column title matches string and value is in limits

I'm somewhat new to BASH scripting but have managed to work my way through most of a problem. I'm trying to get a list of filenames where a column header occurs and any value in that column is within a range. So far I can sort through the list of files in a directory specified by the user, find... (5 Replies)
Discussion started by: hu_r_u2000
5 Replies

8. Shell Programming and Scripting

Finiding filenames with specific index string

Hi All, I have a file (Names.txt) and the contents of the file is give below. $ cat Names.txt FF313207008.txt FF223207007.txt FF143207006.txt FF372150600.txt FF063407005.txt FF063307005.txt $ From these given file names I want to find the files which has the 6th index value as 2. So... (5 Replies)
Discussion started by: krish_indus
5 Replies

9. Shell Programming and Scripting

How to list filenames with spaces in shell script

Hi, I want to list all the files matching in a directory directory given below Here one of the folder has a space in the path. /MAS02/RMS/WBDev/Krishna/Krishna Mohan/FMSplitByKeyAfterChanges1000075383.fileman.*.txt.out.1000075383.0 The following works from command line... (1 Reply)
Discussion started by: hikrishn
1 Replies

10. Shell Programming and Scripting

Getting a list of filenames of moved files

I'm moving a list of files of some extension and I wish to output the moved filenames into a text file, I tried using the command below, but after all the files are moved, I got a blank file. find /abc/temp -type f -mtime +365 \( -name "*.bak" -o -name "*.log" \) -exec mv -f {} /junk \; >>... (3 Replies)
Discussion started by: chengwei
3 Replies
Login or Register to Ask a Question