LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 01-11-2005, 06:18 PM   #1
jpan
Member
 
Registered: Aug 2004
Distribution: Debian Libranet
Posts: 69

Rep: Reputation: 15
How to replace a string in a text file


Hi, I'm using like:


$sed 's/string1/string2/' file1 > out

then, the modified result will be put into out

but when i used:

$sed 's/string1/string2/' file1 > file1


then the file called file1 will become empty!


what's wrong? I just want to replace string1 in file1 to be string2!!


thanks!
 
Old 01-11-2005, 06:28 PM   #2
jpan
Member
 
Registered: Aug 2004
Distribution: Debian Libranet
Posts: 69

Original Poster
Rep: Reputation: 15
also, what about replacing 2 or 3 strings in a text file??? do i just add a
"-e" in the command ??? or i need more stuff??
 
Old 01-11-2005, 07:02 PM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally posted by jpan
$sed 's/string1/string2/' file1 > file1


then the file called file1 will become empty!
When you use the output redirector ( > ), it automatically creates an empty file. The system has to prepare for output first, before any commands are executed, meaning the empty output file has to be created before the sed command. Since your output file and your input file are the same file, it gets wiped out before sed runs.

This is the more traditional way of doing things:
Code:
$ cp file1 file1.bak
$ sed 's/string1/string2/' file1.bak > file1
or
Code:
$ cat file1 | sed 's/string1/string2/' > file1
or
Code:
$ sed -i 's/string1/string2/' file1
or
Code:
$ sed -i.bak 's/string1/string2/' file1
The man pages are your friend.

The last one will create a backup of the original file by appending ".bak" to the end of the original filename. In this case, it would create a file1.bak.

As for multiple string replacement, why not just try it? You're not going to break anything. Experimenting is part of how to learn. But to give a direct answer to your question: yes, you can do multiple replacements in one command. Just preface each 's///' with a -e. The replacements will be executed in order. So it is possible for a later replacement to change the results of an earlier one. For instance
Code:
$ sed -i.bak -e 's/an example/two strings replaced/' -e 's/string/command/' file1
The code above would first change 'an example' to 'two strings replaced', and the second expression would then overwrite the 'string' with 'command', giving 'two commands replaced' as its final output. Other occurences of 'string' in the file will be replaced with 'command' as normal.

Last edited by Dark_Helmet; 01-11-2005 at 07:05 PM.
 
Old 10-14-2012, 07:17 PM   #4
sgmart
LQ Newbie
 
Registered: Jun 2012
Location: Mexico
Distribution: Slackware Linux
Posts: 6

Rep: Reputation: 0
Wink Try this

Quote:
Originally Posted by jpan View Post
Hi, I'm using like:


$sed 's/string1/string2/' file1 > out

then, the modified result will be put into out

but when i used:

$sed 's/string1/string2/' file1 > file1


then the file called file1 will become empty!


what's wrong? I just want to replace string1 in file1 to be string2!!


thanks!
Perhaps very late, but here is:

Code:
$sed 's/string1/string2/' file1 1> file1
Look at the 1> option, this send STDOUT, which is generated by sed, to file1.
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem using sed to replace string in file umk Debian 12 02-01-2012 09:39 AM
How to replace string pattern with multi-line text in bash script? brumela Linux - Newbie 6 04-21-2011 07:56 AM
Python: find defined text string in a file, and replace the whole line Dark Carnival Programming 6 05-22-2007 07:02 AM
Replace text of unknown content with other text in file brian0918 Programming 15 07-14-2005 10:22 PM
replace a string/number in a text file jpan Linux - General 3 10-22-2004 10:33 PM


All times are GMT -5. The time now is 05:40 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration