LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Debian
User Name
Password
Debian This forum is for the discussion of Debian Linux.

Notices


Reply
  Search this Thread
Old 05-12-2005, 10:32 AM   #1
umk
Member
 
Registered: Jan 2005
Distribution: debian (woody)
Posts: 36

Rep: Reputation: 15
Problem using sed to replace string in file


Hi everybody,

I'm trying to write a script which among others has to replace string A in file F with string B. Despite reading the sed man page and some googled stuff, I haven't managed to do this. First, I tried the following command:

sed -e 's/A/B/w F' F

but this simply erases everything from F.

Question 1: why is this?

Then I did the following:

sed -e 's/A/B/g' F >> F.new | rm -f F | mv F.new F

This does what I want (replace A with B in F) but by brute force.

Question 2: Surely there must be a "right" way of doing this. What is it?

Thanks, umk
 
Old 05-12-2005, 10:43 AM   #2
Technoslave
Member
 
Registered: Dec 2003
Location: Northern VA
Posts: 493

Rep: Reputation: 30
You could try something like

sed -i -e 's/dont want/i want' filename

This will replace the string listed above in the file and leave the file as is.
 
Old 05-12-2005, 11:00 AM   #3
umk
Member
 
Registered: Jan 2005
Distribution: debian (woody)
Posts: 36

Original Poster
Rep: Reputation: 15
I've tried it out, but it doesn't do what you say it does. In fact it doesn't do anything but give an error message. Thanks anyway, umk
 
Old 05-12-2005, 12:39 PM   #4
Technoslave
Member
 
Registered: Dec 2003
Location: Northern VA
Posts: 493

Rep: Reputation: 30
Well, give an actual example of what it is you're trying to do. It might be something in the string you're trying to replace and just not escaping out of properly.
 
Old 05-12-2005, 04:37 PM   #5
tibob
LQ Newbie
 
Registered: May 2005
Location: Germany
Distribution: Debian unstable
Posts: 10

Rep: Reputation: 0
Use perl

Hi,

the problem is, you can't use sed to replace in a file, you have to use a temporary file for this.

I use perl for such replaces:

Code:
perl -pi'*.bak' -e 's/stringA/stringB/g' file.txt
Note that it creates a .bak backup file.

For a whole path,
Code:
perl -pi'*.bak' -e 's/stringA/stringB/g' `find path -name '*.txt'`
Cheers,

tibob
 
Old 05-13-2005, 03:31 AM   #6
umk
Member
 
Registered: Jan 2005
Distribution: debian (woody)
Posts: 36

Original Poster
Rep: Reputation: 15
Techonslave,

I've realised that I was using an old version of sed which didn't allow the -i option. I've now got a new version and the command
sed -i 's/old/new/g' filename
does exactly what I want, namely replace old with new in file "filename".

Tibob,

Thanks for the perl tip. When I saw the command line I remembered that I actually used it in one of my first scritps, but then forgot about it. Thanks, umk
 
Old 02-09-2006, 07:52 AM   #7
Marcel Fritzenwallne
LQ Newbie
 
Registered: Feb 2006
Posts: 2

Rep: Reputation: 0
$String replacement doesn't work

Hi,

I want to replace a string in a file, the problem is that the replacement is a variable, and this doesn't work. I could not get it working with sed nor with perl.

Can anyone help me?
----------------------------------------------------------
perl -pi'.bak' -e 's/ABC/$VAR/g' file.txt
----------------------------------------------------------
sed 's/ABC/$VAR/' file.txt
----------------------------------------------------------
--> it doesnt work with $VAR, it doesn't take the variable inside, it replaces "ABC" with the string "$VAR", and this is not what I want.

Thanks a lot!
Marcel


Quote:
Originally Posted by tibob
Hi,

the problem is, you can't use sed to replace in a file, you have to use a temporary file for this.

I use perl for such replaces:

Code:
perl -pi'*.bak' -e 's/stringA/stringB/g' file.txt
Note that it creates a .bak backup file.

For a whole path,
Code:
perl -pi'*.bak' -e 's/stringA/stringB/g' `find path -name '*.txt'`
Cheers,

tibob
 
Old 02-09-2006, 08:47 AM   #8
Marcel Fritzenwallne
LQ Newbie
 
Registered: Feb 2006
Posts: 2

Rep: Reputation: 0
Hi,

Somenone could help me finding the solution, thanks RedHat Mailinglist!
----------------------------------------------------------
sed -i "s/ABC/$VAR/" file.txt
sed -i 's/ABC/'$VAR'/' file.txt
perl -pi'.bak' -e "s/ABC/$VAR/g" file.txt
----------------------------------------------------------
Marcel

Quote:
Originally Posted by Marcel Fritzenwallne
Hi,

I want to replace a string in a file, the problem is that the replacement is a variable, and this doesn't work. I could not get it working with sed nor with perl.

Can anyone help me?
----------------------------------------------------------
perl -pi'.bak' -e 's/ABC/$VAR/g' file.txt
----------------------------------------------------------
sed 's/ABC/$VAR/' file.txt
----------------------------------------------------------
--> it doesnt work with $VAR, it doesn't take the variable inside, it replaces "ABC" with the string "$VAR", and this is not what I want.

Thanks a lot!
Marcel
 
Old 02-13-2006, 07:22 PM   #9
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Rep: Reputation: 49
Me too! Even I am trying the same but with no luck. This is what I am trying with
Code:
perl -i -wpe "s/hdc/$IDEDEVICE/g" /home/test.txt
No luck
 
Old 10-28-2009, 04:40 AM   #10
adjs1157
LQ Newbie
 
Registered: Oct 2009
Distribution: gentoo->fedora->arch linux
Posts: 1

Rep: Reputation: 0
perl/sed replace "in place" with variables

Code:
find . -name '*.txt' -print -exec sed -i s/"${OLD}"/"${NEW}"/g {} \;
or
Code:
find . -name '*.txt' -print -exec perl -pi -e s/"${OLD}"/"${NEW}"/g {} \;
 
Old 02-16-2011, 02:18 AM   #11
shwetha sagar
LQ Newbie
 
Registered: Feb 2011
Posts: 1

Rep: Reputation: 1
sed -i 's/ABC/'$VAR'/g' file.txt

will work..

Last edited by shwetha sagar; 02-16-2011 at 02:19 AM. Reason: not double quotes..
 
1 members found this post helpful.
Old 01-31-2012, 03:39 PM   #12
mohit1611
LQ Newbie
 
Registered: Jan 2012
Posts: 1

Rep: Reputation: Disabled
Smile

Quote:
Originally Posted by shwetha sagar View Post
sed -i 's/ABC/'$VAR'/g' file.txt

will work..
Thanks Swetha. It works
 
Old 02-01-2012, 08:39 AM   #13
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
It's not generally recommended to resurrect old threads, unless you have something important to add to the discussion. Although I suppose you could argue that this qualifies, just barely.

The big issue about using variables in sed is really the same as in many other situations. You have to protect parts of the line that contain spaces or other reserved characters from the shell, while still allowing the variable to expand. In this case, we just need to ensure that the sed expression is passed to the command as a single argument when it is run, with the variable substituted.

The simplest way is to just double-quote the whole thing. Double-quotes escape everything except for "$,`,\" (and "!" history substitution in interactive shells). This allows variable and command substitutions to expand and certain backslash escapes to work, while protecting everything else. So as long as your expression doesn't contain any of the above characters except that of the variable, you can use this:

Code:
sed -i "s/ABC/$VAR/g" file.txt
If there are other reserved characters that need protecting, then you have to hard-quote (or otherwise escape) the parts that need protecting, and un-quote the variable, such as shwetha sagar just posted. But there's a drawback to this. If the string in the variable happens to contain whitespace, then the resulting expression would be word-split into multiple arguments after the substitution was made, and the resulting command would be broken. To guard against that, you should also double-quote the variable to protect the contents.

Let's imagine that there's a "$" in the middle of the matching expression, and a space inside the variable, for example:

Code:
sed -i 's/AB$CD/'"$VAR"'/g' file.txt
Notice how this is getting kind of hard to read. Double-quoting is generally better. Remember you can still use backslash escapes!

Code:
sed -i "s/AB\$CD/$VAR/g" file.txt
For more on handling shell arguments, variables, and whitespace, see here:
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

Last edited by David the H.; 02-01-2012 at 08:42 AM.
 
  


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
How to replace a string in a text file jpan Linux - General 3 10-14-2012 06:17 PM
How can I replace this string with another using sed? dave4545 Programming 7 01-27-2006 10:58 AM
replace a string/number in a text file jpan Linux - General 3 10-22-2004 09:33 PM
[sed] replace string? chuanyung Programming 3 03-11-2004 08:42 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Debian

All times are GMT -5. The time now is 10:56 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
Open Source Consulting | Domain Registration