|
|||||||||
| 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 |
| Tags |
| linux, perl, perl regex, perl shift, regex, shell script, shift, shift perl, ubuntu |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
what errors do you see?
|
| Sponsored Links | |
|
|
#7
|
||||
|
||||
|
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 | ||
|
|
![]() |
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 |
|
|