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 |
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 Hope this helps. |
Is this homework?
|
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 |
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 |
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 |
you might want to read part (or more) of this: http://coewww.rutgers.edu/www1/linux...ute/index.html
|
the first step to be efficient in shell script: Know your Shell! after that, learn the tools..
|
The manual for coreutils has instructions on almost 100 common commands, like cat, split, sort, etc. It is a must read.
|
Here you go
http://sed.sourceforge.net/sed1line.txt |
use sed command : (substitute)
eg: sed s#\"FindThis\"#\"ReplaceWithThis\"# \"InThisFile\" |
Quoth tinkster:
Quote:
|
Quote:
and solve homework issues for others. The stocks, the stocks!! :D Cheers, Tink |
Is this one OK?
sed 's/searchtext/DO YOUR OWN HOMEWORK! That is how you learn./' |
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. |