![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 01:33 PM |
| using grep and print filename | ahjiefreak | Shell Programming and Scripting | 5 | 01-10-2008 07: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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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.
|
| Forum Sponsor | ||
|
|
|
|||
|
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
|
|
|||
|
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
|
|||
| Google The UNIX and Linux Forums |