LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 06-20-2007, 12:05 PM   #1
unixlearner
LQ Newbie
 
Registered: Jun 2007
Posts: 2

Rep: Reputation: 0
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
 
Old 06-20-2007, 12:21 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
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.

Last edited by wjevans_7d1@yahoo.co; 06-20-2007 at 12:23 PM.
 
Old 06-20-2007, 12:22 PM   #3
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
Is this homework?
 
Old 06-20-2007, 12:59 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
Old 06-20-2007, 01:54 PM   #5
unixlearner
LQ Newbie
 
Registered: Jun 2007
Posts: 2

Original Poster
Rep: Reputation: 0
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
 
Old 06-20-2007, 04:53 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
Old 06-21-2007, 02:09 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,342

Rep: Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746
you might want to read part (or more) of this: http://coewww.rutgers.edu/www1/linux...ute/index.html
 
Old 06-21-2007, 04:16 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
the first step to be efficient in shell script: Know your Shell! after that, learn the tools..
 
Old 06-21-2007, 04:51 AM   #9
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
The manual for coreutils has instructions on almost 100 common commands, like cat, split, sort, etc. It is a must read.
 
Old 06-21-2007, 05:12 AM   #10
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
Here you go
http://sed.sourceforge.net/sed1line.txt
 
Old 06-21-2007, 07:19 AM   #11
kambrish
LQ Newbie
 
Registered: Jun 2007
Location: India
Posts: 12

Rep: Reputation: 0
use sed command : (substitute)

eg:

sed s#\"FindThis\"#\"ReplaceWithThis\"# \"InThisFile\"
 
Old 06-21-2007, 02:00 PM   #12
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
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.
 
Old 06-21-2007, 06:21 PM   #13
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
Old 06-21-2007, 07:26 PM   #14
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Is this one OK?

sed 's/searchtext/DO YOUR OWN HOMEWORK! That is how you learn./'
 
Old 06-21-2007, 10:37 PM   #15
kambrish
LQ Newbie
 
Registered: Jun 2007
Location: India
Posts: 12

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Does anyone know of a bash script can search & replace txt in a file. jimwelc Linux - Newbie 6 09-15-2008 12:13 AM
Trying to make a script that will search and replace text in a file Jongi Programming 28 07-08-2007 12:37 PM
Help! Script or commanded needed to replace text in a file farmerjoe Programming 3 01-02-2005 05:59 PM
Script to search and replace in text file - kinda... jeffreybluml Programming 45 11-07-2004 05:37 PM
Search and replace text in file using shell script? matthurne Linux - Software 2 11-02-2004 10:11 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration