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 > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl: Search for string on line then search and replace text Crypto Shell Programming and Scripting 4 01-04-2008 10:24 AM
Search and Replace in Ksh DeepakXavier Shell Programming and Scripting 9 05-28-2007 08:11 AM
sed search and replace d__browne UNIX for Dummies Questions & Answers 7 04-26-2006 09:46 AM
how to search for a str and replace it.. sekar sundaram Shell Programming and Scripting 6 12-29-2005 06:11 PM
Search and replace sed or tr bridgeje Shell Programming and Scripting 6 10-28-2003 07:54 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 09-14-2001
mukeshannamalai mukeshannamalai is offline
Registered User
  
 

Join Date: Sep 2001
Location: India
Posts: 2
Thumbs up search and Replace

hello,

How to search a Word in a shell file and if that exists echo that the word exists and stop that operation and if that doesn't exists then echo word not found.

Thanks in advance.

Bye
  #2 (permalink)  
Old 09-14-2001
LivinFree's Avatar
LivinFree LivinFree is offline Forum Advisor  
Goober Extraordinaire
  
 

Join Date: Jul 2001
Location: Portland, OR, USA
Posts: 1,584
Code:
#!/bin/sh
# check.sh
# This script takes two arguments -
# The first is the word you a looking for,
# and the second is the file you want to find
# the word in.
# Example: check.sh lamb haggis
# Will look for the word "lamb" in the file "haggis"
if [ "$#" -ne "2" ] ; then
     echo "Usage: "
     echo "`basename $0` {search string} {file} "
     exit 2
fi
if [ -f "$2" ] ; then
grep "$1" $2 > /dev/null 
case $? in
     0) echo -e "\nThe word \"$1\" was found in the \"$2\" file! \n" ;;
     *) echo -e "\nThe word \"$1\" was NOT found in the \"$2\" file! \n" ;;
esac
exit 0
else
echo "The \"$2\" file does not exist"
exit 2
fi
This is one of many ways to do it...

Last edited by LivinFree; 09-14-2001 at 03:16 AM..
  #3 (permalink)  
Old 09-14-2001
LivinFree's Avatar
LivinFree LivinFree is offline Forum Advisor  
Goober Extraordinaire
  
 

Join Date: Jul 2001
Location: Portland, OR, USA
Posts: 1,584
I was just thinking about it, and I guess this isn't the most helpful was to explain this. I'm going to add some comments (in bold) to the code, so you can see what's happening step by step:
Quote:
Originally posted by LivinFree
Code:
#!/bin/sh
# check.sh
# This script takes two arguments -
# The first is the word you a looking for,
# and the second is the file you want to find
# the word in.
# Example: check.sh lamb haggis
# Will look for the word "lamb" in the file "haggis"
# This is to make sure there are exactly two arguments
# passed to the script. If not, it will give you a little hint...
# The "$#" counts the number of aguments, and -ne is the same
# as saying "not equal".
if [ "$#" -ne "2" ] ; then
     echo "Usage: "
     echo "`basename $0` {search string} {file} "
     exit 2
fi
[/b]# Right here, we check to make sure the file we are searching
# exists, and is a regular file. For more information on the -f
# statement, use the 'man test' command.[/b]
if [ -f "$2" ] ; then
# We don't actually want to see the results of the search,
# we only want to know if it was successful, so we send the
# output to /dev/null - to nowhere-land
grep "$1" $2 > /dev/null 
# The "$?" construct tells you the status of the last command.
# if $? is equal to zero, the command was successful, if it is any
# number other than 0, there was a problem
case $? in
     0) echo -e "\nThe word \"$1\" was found in the \"$2\" file! \n" ;;
     *) echo -e "\nThe word \"$1\" was NOT found in the \"$2\" file! \n" ;;
esac
exit 0
# This is what happens if the file did not exist (see above)
# This is our backup plan - it keeps grep from getting "stuck"
# if there is no file
else
echo "The \"$2\" file does not exist"
exit 2
fi
The exit 0 tells it to exit successfully (remember $?), and exit 2 tells it that there were errors, and to exit right away. I hope this is a little more helpful
This is one of many ways to do it...
By saying this is one of many, I mean it - here is an example of a one-liner that will do nearly the same thing without the error handling or variables:
Code:
grep lamb haggis > /dev/null && echo Word found in file || echo Word NOT found in file
As you can see, the "grep" command is the same as in the script, but the messages are handled differently. The commands after the "&&" will execute if the previous command was successful, and the command after the "||" will execute if it was not successful.

Geez, I'd make a horrible teacher...

Last edited by LivinFree; 09-14-2001 at 03:54 AM..
  #4 (permalink)  
Old 09-14-2001
mukeshannamalai mukeshannamalai is offline
Registered User
  
 

Join Date: Sep 2001
Location: India
Posts: 2
hello,

consider searching a word without passing any parameter , but only giving single constant word and single file.

thanks.
  #5 (permalink)  
Old 09-14-2001
LivinFree's Avatar
LivinFree LivinFree is offline Forum Advisor  
Goober Extraordinaire
  
 

Join Date: Jul 2001
Location: Portland, OR, USA
Posts: 1,584
Well, if you want to hard-code the names into the script, that would be even easier...

Code:
#!/bin/sh
grep {your_word} {file} > /dev/null && echo "Word found" || echo "Word not found"
Very similar to the example I gave above, simply insert into a file and execute...
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 04:22 PM.


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