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
read list of filenames from text file and remove these files in multiple directories fxvisions Shell Programming and Scripting 5 08-07-2008 03:59 PM
How can I rename multiple files depending on a string occuring in the filenames? karman UNIX for Dummies Questions & Answers 6 05-22-2007 02:29 PM
Getting a list of filenames of moved files chengwei Shell Programming and Scripting 3 04-23-2007 03:25 AM
read list of filenames from text file, archive, and remove fxvisions Shell Programming and Scripting 5 03-20-2007 09:56 PM
String replace perl script error MobileUser Shell Programming and Scripting 6 05-04-2006 09:18 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 04-21-2008
royalibrahim royalibrahim is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 86
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 (permalink)  
Old 04-21-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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 (permalink)  
Old 04-21-2008
royalibrahim royalibrahim is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 86
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 03:14 AM..
  #4 (permalink)  
Old 04-21-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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 03:11 AM.. Reason: Skip files which don't match /\.txt$/
  #5 (permalink)  
Old 04-21-2008
royalibrahim royalibrahim is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 86
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 (permalink)  
Old 04-21-2008
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
what errors do you see?
Closed Thread

Bookmarks

Tags
linux, perl, perl regex, perl shift, regex, shell script, shift, shift perl, ubuntu

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 07:00 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