Sponsored Content
Full Discussion: search and Replace
Top Forums UNIX for Advanced & Expert Users search and Replace Post 6874 by LivinFree on Friday 14th of September 2001 03:39:58 AM
Old 09-14-2001
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 04:54 AM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

search and replace

hi all, the problem is like this...... i setup a file (Env.txt) which handles all the values. NAME1=xxxxxx, where xxxxx is the value NAME2=xxxxxx GGGGG=uusufu I have 6 files, where i will append the values from env.txt. These files has no specific format. all i want is to append the... (1 Reply)
Discussion started by: tungaw2004
1 Replies

2. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

3. Shell Programming and Scripting

Search and replace

I have a script which has several occurences of statement like command: $UCMDPATH/xyz abc I would like to replace the entire line starting from $UCMDPATH with the ls -ltr command eg: Prior to change:: command: $UCMDPATH/xyz abc After change: command: ls -ltr Is there a way to do... (12 Replies)
Discussion started by: jobbyjoseph
12 Replies

4. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

5. Shell Programming and Scripting

Search and Replace

Hi!!! I have following xml file with 3 sections. aaa1bbb-ccc default aaa1bbbdd default 0 11 23 * * aaa2bbb-ccc default aaa2bbbdd default 0 11 23 * * aaa3bbb-ccc default aaa3bbbdd default 0 15 23 * * (15 Replies)
Discussion started by: tdev457
15 Replies

6. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

7. UNIX for Dummies Questions & Answers

Search and Replace

I am trying to search and replace using two different files with strict search rules. One file contain some data and the other file contain some numbers as shown below. DataFile.txt >L1_T1 text data... text data.. >L1_T1 text data... text data.. >L1_T1 text data... text data.. ........ (4 Replies)
Discussion started by: Fahmida
4 Replies

8. UNIX for Dummies Questions & Answers

Help with search and replace or search only of / in vi

Hi all, I am editing a config file in vi that has a / on it. At the moment, search and replace looks alright as am able to use a # as a temporary separator, i.e. :,$s#/u01/app#/u02/app#g For doing a search, I have to escape the / do. So if I want to search for /u01/app, I am having to do... (2 Replies)
Discussion started by: newbie_01
2 Replies

9. Shell Programming and Scripting

Vi search and replace

Hi, I need help with search and replace for the following string using vi .. if ];then to if ];then can this be done in one search and replace or need multiple ? Thanks (3 Replies)
Discussion started by: talashil
3 Replies

10. Shell Programming and Scripting

Nested search in a file and replace the inner search

Hi Team, I am new to unix, please help me in this. I have a file named properties. The content of the file is : ##Mobile props east.url=https://qa.east.corp.com/prop/end west.url=https://qa.west.corp.com/prop/end south.url=https://qa.south.corp.com/prop/end... (2 Replies)
Discussion started by: tolearn
2 Replies
escape(1)							Mail Avenger 0.8.3							 escape(1)

NAME
escape - escape shell special characters in a string SYNOPSIS
escape string DESCRIPTION
escape prepends a "" character to all shell special characters in string, making it safe to compose a shell command with the result. EXAMPLES
The following is a contrived example showing how one can unintentionally end up executing the contents of a string: $ var='; echo gotcha!' $ eval echo hi $var hi gotcha! $ Using escape, one can avoid executing the contents of $var: $ eval echo hi `escape "$var"` hi ; echo gotcha! $ A less contrived example is passing arguments to Mail Avenger bodytest commands containing possibly unsafe environment variables. For example, you might write a hypothetical reject_bcc script to reject mail not explicitly addressed to the recipient: #!/bin/sh formail -x to -x cc -x resent-to -x resent-cc | fgrep "$1" > /dev/null && exit 0 echo "<$1>.. address does not accept blind carbon copies" exit 100 To invoke this script, passing it the recipient address as an argument, you would need to put the following in your Mail Avenger rcpt script: bodytest reject_bcc `escape "$RECIPIENT"` SEE ALSO
avenger(1), The Mail Avenger home page: <http://www.mailavenger.org/>. BUGS
escape is designed for the Bourne shell, which is what Mail Avenger scripts use. escape might or might not work with other shells. AUTHOR
David Mazieres Mail Avenger 0.8.3 2012-04-05 escape(1)
All times are GMT -4. The time now is 10:28 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy