how to do something if grep string is there | 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 do something if grep string is there

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-30-2008
borderblaster borderblaster is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 4 May 2011, 5:21 PM EDT
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
how to do something if grep string is there

hi in the script below i want to grep for the word not, if true i want to do something. its not working? I want it to do something if grep finds a match for the string 'not'. At the minute is doing

./test.sh: line 3: [: too many arguments

yes
[root@local ~]# vi test.sh
test=`cat latest.txt | grep 'not'`
echo $test
if [ `cat latest.txt | grep not` ]
then
echo yes
else
echo no
fi:

Last edited by borderblaster; 10-30-2008 at 06:57 PM..
Sponsored Links
    #2  
Old 10-30-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 30 July 2014, 10:30 AM EDT
Location: NM
Posts: 10,159
Thanks: 269
Thanked 781 Times in 731 Posts
far simpler like this

Code:
grep -q 'not' somefile
if [[ $? -eq 0 ]] ; then  # we found a not
    # do stuff to file
fi

You do realize that grep 'not' matches words like "nothing" and "whatnot" as well as "not" ?
The Following User Says Thank You to jim mcnamara For This Useful Post:
figaro (07-22-2010)
Sponsored Links
    #3  
Old 10-30-2008
borderblaster borderblaster is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 4 May 2011, 5:21 PM EDT
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
cheers jim this worked brilliantly. i changed the 'not' to something more unique.
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
Grep a string from input file and delete next three lines including the line contains string in xml greet_sed Shell Programming and Scripting 4 09-28-2011 05:18 AM
Grep a string and write a value to next line of found string angel12345 Shell Programming and Scripting 6 08-16-2011 11:07 AM
grep on string and printing line after until another string has been found rxc23816 Shell Programming and Scripting 7 07-23-2011 05:49 PM
Grep for a string and then grep using a string from that result kzenthil Shell Programming and Scripting 8 02-18-2011 10:52 PM
| help | unix | grep - Can I use grep to return a string with exactly n matches? MykC UNIX for Dummies Questions & Answers 7 10-13-2009 01:57 PM



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