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
How to grep / zgrep to output ONLY the matching filename and line number? vvaidyan UNIX for Dummies Questions & Answers 3 03-12-2008 05:33 PM
using grep and print filename ahjiefreak Shell Programming and Scripting 5 01-10-2008 11:47 AM
Returning filename and matching lines smb_uk Shell Programming and Scripting 2 10-05-2007 06:08 PM
Grep Line with Matching Fields hemangjani UNIX for Advanced & Expert Users 13 08-10-2007 11:46 AM
perl pattern matching vs. grep junkmail426 Shell Programming and Scripting 0 09-28-2005 10:40 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-07-2005
mharley mharley is offline
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.
  #2 (permalink)  
Old 06-07-2005
jolok jolok is offline
Registered User
  
 

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

That's really what grep is for; please read the grep manpage.
  #3 (permalink)  
Old 06-08-2005
mharley mharley is offline
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?
  #4 (permalink)  
Old 06-08-2005
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
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.
Closed Thread

Bookmarks

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 10:57 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