The UNIX and Linux Forums  

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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to grep / zgrep to output ONLY the matching filename and line number? vvaidyan UNIX for Dummies Questions & Answers 3 03-12-2008 02:33 PM
using grep and print filename ahjiefreak Shell Programming and Scripting 5 01-10-2008 08:47 AM
Returning filename and matching lines smb_uk Shell Programming and Scripting 2 10-05-2007 03:08 PM
Grep Line with Matching Fields hemangjani UNIX for Advanced & Expert Users 13 08-10-2007 08:46 AM
perl pattern matching vs. grep junkmail426 Shell Programming and Scripting 0 09-28-2005 07:40 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 06-07-2005
Registered User
 

Join Date: Mar 2005
Posts: 14
Grep all files matching partial filename

What would be the easiest way to grep all files within a particular directory that match a partial filename? For example, searching all files that begin with "filename.txt" and are appended with the date they were created. I am using Ksh 88, btw.
Reply With Quote
Forum Sponsor
  #2  
Old 06-07-2005
Registered User
 

Join Date: Mar 2005
Posts: 64
grep "expression"

That's really what grep is for; please read the grep manpage.
Reply With Quote
  #3  
Old 06-08-2005
Registered User
 

Join Date: Mar 2005
Posts: 14
What I would ideally like to do is perform a grep on all of the files, and if any of the search parameters are found, output both the filename and the line containing the parameter found. This is the default behaviour for grep, is it not? If no matches are found, I want to output an appropriate message.

I was thinking of something along the lines of:
Code:
if grep ${vString} filename.ext.* > /dev/null
then
    grep ${vString} filename.ext.*
else
    print "${vString} not found in ${instances[x]} filename.ext"
fi
Running that line of code twice seems wasteful, though. Can this be done without the duplication?
Reply With Quote
  #4  
Old 06-08-2005
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,269
Try something like this:
Code:
#!/bin/ksh

for file in `ls -1 filename.ext.*`
do 
    result=`grep ${vString} $file`
    if [ $? -ne 0 ] ; then
        echo "${vString} not found in ${instances[x]} -- in $file"
    else
        echo "$file $result"    
    fi
done
-- not tested.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:49 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0