LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Need a script to search and replace text in file using shell script (https://www.linuxquestions.org/questions/programming-9/need-a-script-to-search-and-replace-text-in-file-using-shell-script-563241/)

unixlearner 06-20-2007 12:05 PM

Need a script to search and replace text in file using shell script
 
Hi Gurus,

I am very new to unix and currently learning.

I need a help. My concern is how to find a text in a file and replace the same with the another text value. The find and replace text values will be passed through the command propmpt something like:

searchreplace.sh <searchtext arg> <replacetext arg>

Appreciate if your help in advance.

Thanks,

Smitha

wjevans_7d1@yahoo.co 06-20-2007 12:21 PM

The first step in scripting is figuring out exactly what you want done. Exactly.

Sure, you want to replace text, and you attempt to show how this would work on the command line. A good first step.

But do you want to specify the input and output file names in that command also? Or is that command just supposed to be a filter (in which case a simple, one-line sed command should work, with no script necessary)?

Or what?

If you don't know what a "filter" is, drop everything and google:

Code:

bash tutorial
because there is much you need to read before starting out.

Hope this helps.

nx5000 06-20-2007 12:22 PM

Is this homework?

Tinkster 06-20-2007 12:59 PM

Looks like it to me :}

Someone w/o an idea of shell-scripting wouldn't have worded
the request like:
searchreplace.sh <searchtext arg> <replacetext arg>


:}

Cheers,
Tink

unixlearner 06-20-2007 01:54 PM

Thanks for your replies.

Yes it is a home work and Tinkster is correct.

Tinkster comments
Someone w/o an idea of shell-scripting wouldn't have worded
the request like:
searchreplace.sh <searchtext arg> <replacetext arg>

Appreciate if you could let me know what should be the correct wording? Afterall, it is a learning curve to me :)

Smitha

Tinkster 06-20-2007 04:53 PM

Well.... the wording is fine :} ... it just suggests that those aren't
your own words as it contradicts the statement that you're new to
scripting.

To be efficient with shell scripting you need to know the basic
command line tools. Read the man pages for grep, sed, and maybe
awk. Also scour this forum for those tools. You'll find tons
of useful examples.

And no, no one here will do your homework for you :}



Cheers,
Tink

chrism01 06-21-2007 02:09 AM

you might want to read part (or more) of this: http://coewww.rutgers.edu/www1/linux...ute/index.html

ghostdog74 06-21-2007 04:16 AM

the first step to be efficient in shell script: Know your Shell! after that, learn the tools..

jschiwal 06-21-2007 04:51 AM

The manual for coreutils has instructions on almost 100 common commands, like cat, split, sort, etc. It is a must read.

nc3b 06-21-2007 05:12 AM

Here you go
http://sed.sourceforge.net/sed1line.txt

kambrish 06-21-2007 07:19 AM

use sed command : (substitute)

eg:

sed s#\"FindThis\"#\"ReplaceWithThis\"# \"InThisFile\"

wjevans_7d1@yahoo.co 06-21-2007 02:00 PM

Quoth tinkster:

Quote:

And no, no one here will do your homework for you :}
There's a reason for this. kambrish, you're not helping.

Tinkster 06-21-2007 06:21 PM

Quote:

Originally Posted by kambrish
use sed command : (substitute)

eg:

sed s#\"FindThis\"#\"ReplaceWithThis\"# \"InThisFile\"

OK, it appears that we do have new eager beavers who ignore rules
and solve homework issues for others. The stocks, the stocks!! :D



Cheers,
Tink

jschiwal 06-21-2007 07:26 PM

Is this one OK?

sed 's/searchtext/DO YOUR OWN HOMEWORK! That is how you learn./'

kambrish 06-21-2007 10:37 PM

i am sorry if i have break the rules. i was just trying to resolve the query. Will take care next time!!


All times are GMT -5. The time now is 12:08 AM.