Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
06-20-2007, 12:05 PM
|
#1
|
LQ Newbie
Registered: Jun 2007
Posts: 2
Rep:
|
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
|
|
|
06-20-2007, 12:21 PM
|
#2
|
Member
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938
Rep:
|
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:
because there is much you need to read before starting out.
Hope this helps.
Last edited by wjevans_7d1@yahoo.co; 06-20-2007 at 12:23 PM.
|
|
|
06-20-2007, 12:22 PM
|
#3
|
Senior Member
Registered: Sep 2005
Location: Out
Posts: 3,307
Rep:
|
Is this homework?
|
|
|
06-20-2007, 12:59 PM
|
#4
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
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
|
|
|
06-20-2007, 01:54 PM
|
#5
|
LQ Newbie
Registered: Jun 2007
Posts: 2
Original Poster
Rep:
|
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
|
|
|
06-20-2007, 04:53 PM
|
#6
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
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
|
|
|
06-21-2007, 02:09 AM
|
#7
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,398
|
|
|
|
06-21-2007, 04:16 AM
|
#8
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
the first step to be efficient in shell script: Know your Shell! after that, learn the tools..
|
|
|
06-21-2007, 04:51 AM
|
#9
|
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
The manual for coreutils has instructions on almost 100 common commands, like cat, split, sort, etc. It is a must read.
|
|
|
06-21-2007, 05:12 AM
|
#10
|
Member
Registered: Aug 2005
Posts: 330
Rep:
|
|
|
|
06-21-2007, 07:19 AM
|
#11
|
LQ Newbie
Registered: Jun 2007
Location: India
Posts: 12
Rep:
|
use sed command : (substitute)
eg:
sed s#\"FindThis\"#\"ReplaceWithThis\"# \"InThisFile\"
|
|
|
06-21-2007, 02:00 PM
|
#12
|
Member
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938
Rep:
|
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.
|
|
|
06-21-2007, 06:21 PM
|
#13
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
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
|
|
|
06-21-2007, 07:26 PM
|
#14
|
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
Is this one OK?
sed 's/searchtext/DO YOUR OWN HOMEWORK! That is how you learn./'
|
|
|
06-21-2007, 10:37 PM
|
#15
|
LQ Newbie
Registered: Jun 2007
Location: India
Posts: 12
Rep:
|
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 01:20 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|