LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 05: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, 05: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, 06:02 PM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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 06:05 PM.
 
Old 10-14-2012, 06:17 PM   #4
sgmart
LQ Newbie
 
Registered: Jun 2012
Location: Mexico
Distribution: Slackware Linux
Posts: 9

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



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 08:39 AM
How to replace string pattern with multi-line text in bash script? brumela Linux - Newbie 6 04-21-2011 06:56 AM
Python: find defined text string in a file, and replace the whole line Dark Carnival Programming 6 05-22-2007 06:02 AM
Replace text of unknown content with other text in file brian0918 Programming 15 07-14-2005 09:22 PM
replace a string/number in a text file jpan Linux - General 3 10-22-2004 09:33 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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