How to search (grep?) filename for a string and if it contains this then... | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

How to search (grep?) filename for a string and if it contains this then...

Shell Programming and Scripting


Tags
grep file search if

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-18-2008
tuathan tuathan is offline
Registered User
 
Join Date: Nov 2008
Last Activity: 29 November 2010, 1:34 AM EST
Posts: 12
Thanks: 2
Thanked 0 Times in 0 Posts
How to search (grep?) filename for a string and if it contains this then...

Hi i want to write a script that will search a filename e.g. test06abc.txt for a string and if it contains this string then set a variable equal to something:

something like:

var1=0

search <filename> for 06
if it contains 06 then
var1=1
else
var1=0
end if

but in unix script
Sponsored Links
    #2  
Old 11-19-2008
mailme0712 mailme0712 is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 8 May 2009, 1:59 AM EDT
Location: Chennai,India
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
using find command followed by if condition

Hi try this

file=`find dirpath -type f -name "filename" -exec grep 06 {} \;`

if [ "$file" = "" ] ; then
var=0
else
var=1
fi

Last edited by mailme0712; 11-19-2008 at 04:04 AM..
Sponsored Links
    #3  
Old 11-19-2008
yongitz yongitz is offline
Registered User
 
Join Date: Apr 2008
Last Activity: 8 December 2011, 4:19 PM EST
Location: Philippines
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
Hi! You can use grep for this.


Code:
check=`grep 06 FILENAME > /dev/null; echo $?`
if [ "$check" -eq "0" ]; then
var1=1
else
var1=0
fi

    #4  
Old 11-19-2008
Christoph Spohr Christoph Spohr is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 29 September 2011, 1:45 PM EDT
Posts: 246
Thanks: 0
Thanked 8 Times in 8 Posts
Hi,

grep has the -q or --silent option for this. Try


Code:
grep -q "test" file && a=found || a=missing

which will set $a to found if "test" was found in file. Else $a will be set to missing.

HTH Chris
Sponsored Links
    #5  
Old 11-19-2008
trutoman's Avatar
trutoman trutoman is offline
Registered User
 
Join Date: Nov 2008
Last Activity: 16 May 2013, 7:30 PM EDT
Posts: 30
Thanks: 1
Thanked 0 Times in 0 Posts
When i search for a string in any file i use this:


Code:
find ./ -name "filename" | xargs grep "string_to_found"

In this way you can specify what kind of files are you searching for
"filename" or "file*" or "?ile_+.txt" or .....

it's clear and simple.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to search for string A OR string B using Grep? jboy UNIX for Dummies Questions & Answers 5 04-18-2011 10:36 AM
Search for string in filename, not file content daflore Shell Programming and Scripting 3 04-19-2010 10:03 AM
grep for a search string raga UNIX for Dummies Questions & Answers 11 03-12-2008 08:38 AM
grep string and output filename happyv Shell Programming and Scripting 3 11-19-2007 11:16 PM
Appending to filename a string of text grep finds HLee1981 Shell Programming and Scripting 3 09-06-2005 02:44 PM



All times are GMT -4. The time now is 03:11 PM.