|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | 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 and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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 | ||
|
![]() |
| Tags |
| grep file search if |
| Thread Tools | Search this Thread |
| 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 |
|
|