Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

learn unix and linux commands

perl script to list filenames that do not contain given string

Shell Programming and Scripting


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

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 04-21-2008   -   Original Discussion by royalibrahim
royalibrahim's Unix or Linux Image
royalibrahim royalibrahim is offline
Registered User
 
Join Date: Jun 2007
Last Activity: 22 November 2017, 3:56 AM EST
Posts: 353
Thanks: 56
Thanked 5 Times in 5 Posts
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.
Sponsored Links
    #2  
Old Unix and Linux 04-21-2008   -   Original Discussion by royalibrahim
era's Unix or Linux Image
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 11 Times in 9 Posts
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.
Sponsored Links
    #3  
Old Unix and Linux 04-21-2008   -   Original Discussion by royalibrahim
royalibrahim's Unix or Linux Image
royalibrahim royalibrahim is offline
Registered User
 
Join Date: Jun 2007
Last Activity: 22 November 2017, 3:56 AM EST
Posts: 353
Thanks: 56
Thanked 5 Times in 5 Posts
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 Unix and Linux 04-21-2008   -   Original Discussion by royalibrahim
era's Unix or Linux Image
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 11 Times in 9 Posts
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$/
Sponsored Links
    #5  
Old Unix and Linux 04-21-2008   -   Original Discussion by royalibrahim
royalibrahim's Unix or Linux Image
royalibrahim royalibrahim is offline
Registered User
 
Join Date: Jun 2007
Last Activity: 22 November 2017, 3:56 AM EST
Posts: 353
Thanks: 56
Thanked 5 Times in 5 Posts
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?
Sponsored Links
    #6  
Old Unix and Linux 04-21-2008   -   Original Discussion by royalibrahim
Yogesh Sawant's Unix or Linux Image
Yogesh Sawant Yogesh Sawant is offline Forum Advisor  
Full Time Dad
 
Join Date: Sep 2006
Last Activity: 20 December 2017, 6:02 AM EST
Location: Rossem, Tazenda
Posts: 1,213
Thanks: 7
Thanked 18 Times in 17 Posts
what errors do you see?
Sponsored Links
    #7  
Old Unix and Linux 04-21-2008   -   Original Discussion by royalibrahim
royalibrahim's Unix or Linux Image
royalibrahim royalibrahim is offline
Registered User
 
Join Date: Jun 2007
Last Activity: 22 November 2017, 3:56 AM EST
Posts: 353
Thanks: 56
Thanked 5 Times in 5 Posts
Quote:
Originally Posted by Yogesh Sawant View Post
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)
Sponsored Links
Closed


Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Find the list of filenames that have the string 31 at 4th and 5th position okkadu UNIX for Dummies Questions & Answers 8 04-11-2012 05:55 PM
List of filenames where column title matches string and value is in limits hu_r_u2000 Shell Programming and Scripting 5 09-20-2011 01:46 PM
String compare with list in shell script rajinavaneethan Shell Programming and Scripting 2 06-11-2009 11:10 PM
How to list filenames with spaces in shell script hikrishn Shell Programming and Scripting 1 06-18-2008 05:33 PM
Getting a list of filenames of moved files chengwei Shell Programming and Scripting 3 04-23-2007 04:25 AM



All times are GMT -4. The time now is 04:08 AM.