LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-27-2010, 07:26 AM   #1
adarshmca
LQ Newbie
 
Registered: Dec 2010
Posts: 28

Rep: Reputation: 0
find or replace through sed


Dear all,


I am successfully execute the below command in my script :

sed '/findstring/s//replacestring/g' find1.txt > /root/Desktop/output1.txt

but when I want to replace it with some runtime input it fails .
I want to accept a string from user and then find, replace it.
Below is my script :

#/bin/bash
#Script for Find and Replace

#filename=$1

findstring=$1

replacestring=$2

echo $findstring
echo $replacestring

#sed '/$findstring/s//$replacestring/g' find1.txt > /root/Desktop/output1.txt



# Below line executed successfully.

#sed '/tea/s//milk/g' find1.txt > /root/Desktop/output1.txt


#sed -i 's/findstring/replacestring/g' *

echo "Script executed successfully"



Pls help how could I achieve this.


Thanks & Regards

Adarsh Sharma
 
Old 12-27-2010, 07:41 AM   #2
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 48
To make the shell handle your variables you need to use " instead of '

i.e.

sed "/$findstring/s//$replacestring/g" find1.txt > /root/Desktop/output1.txt

Your syntax looks a bit strange to me. I'm used to writing it a bit differently:

sed "s/$findstring/$replacestring/g"

Last edited by alienDog; 12-27-2010 at 07:43 AM.
 
1 members found this post helpful.
Old 12-27-2010, 11:18 PM   #3
adarshmca
LQ Newbie
 
Registered: Dec 2010
Posts: 28

Original Poster
Rep: Reputation: 0
Thanks a lot ! It works.


Best Regards

Adarsh Sharma
 
Old 12-27-2010, 11:35 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Please mark as SOLVED once you have a working solution.
 
  


Reply



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
problems using sed to find and replace.... tdnnash25 Linux - Server 4 03-01-2010 06:34 PM
Find several different words and replace with one using sed. Techno Guy Linux - Newbie 18 07-06-2009 07:16 AM
Use sed to find and replace a url xmrkite Linux - Software 4 10-10-2007 07:20 PM
sed - find and replace command bullshit Programming 9 01-05-2006 03:25 AM
SED;find and replace;help required gd13 Programming 3 12-21-2004 06:33 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:39 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