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
# 1  
Old 04-21-2008
perl script to list filenames that do not contain given string

Hi,

I need to write a perl script that should do a search recursively in all the '*.txt' files for the string "ALL -Tcb" and should print only the file names that do not contain this string.
# 2  
Old 04-21-2008
Why Perl? It can be done, of course, but it sounds easier in shell.

Code:
find . -type f -name '*.txt' -print |
while read file; do
  grep -l "ALL -Tcb" "$file" >/dev/null && continue
  echo "$file"
done

If you have a grep which understands the -q option, use that instead of -l and the redirection to /dev/null.
# 3  
Old 04-21-2008
Thanks era, but I am not using Unix or Linux machines rather Cygwin on windows. And it is a requirement for me to develop it only in Perl.

Last edited by royalibrahim; 04-21-2008 at 04:14 AM..
# 4  
Old 04-21-2008
Hmm, this is a Unix forum ...?

Code:
perl -MFile::Find -e 'find(sub {
  return 0 unless (m/\.txt$/);  # skip file names which don't match this regex
  open (F, $File::Find::name) || warn "could not open $File::Find::name: $!\n";
  my $match = grep { /ALL -Tcb/ } <F>;
  close F;
  print "$File::Find::name\n" unless $match;
  return ! $match; }, ".")'

See the File::Find documentation for a bit of background. The grep will return a list of matching lines; because it is invoked in scalar context, that list will be turned into the number of elements in the list of matches. If that number is zero, there were none, and we print the file name.

The final parameter is the list of directories to traverse; simply "." will traverse the current directory and its subdirectories.

Last edited by era; 04-21-2008 at 04:11 AM.. Reason: Skip files which don't match /\.txt$/
# 5  
Old 04-21-2008
Thanks a lot era. If I run this script at command line of Cygwin, it is spewing lot many errors. So could you change it and give me as a perl program, so that I can run it from a file?
# 6  
Old 04-21-2008
what errors do you see?
# 7  
Old 04-21-2008
Quote:
Originally Posted by Yogesh Sawant
what errors do you see?
could not open ./api/group/file.txt: No such file or directory

like this continuously where ever txt files are found. I ran it by copying and pasting each and every line of the script (removed the comment)
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