LinuxQuestions.org
Visit Jeremy's Blog.
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, 08: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, 08:41 AM   #2
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 46
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 08:43 AM.
 
1 members found this post helpful.
Old 12-28-2010, 12:18 AM   #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-28-2010, 12:35 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

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


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


All times are GMT -5. The time now is 03:16 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration