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 10-22-2004, 08:53 PM   #1
jpan
Member
 
Registered: Aug 2004
Distribution: Debian Libranet
Posts: 69

Rep: Reputation: 15
replace a string/number in a text file


Hi, anybody knows that


how to replace a token(string or number) in a text file with a certain token(string/number) by a single line of command (might be combinations, pipelines....etc.)?? i want to do it in my bash shell scripts.


thanks!!


Jimmy
 
Old 10-22-2004, 10:13 PM   #2
DavidPhillips
Guru
 
Registered: Jun 2001
Location: South Alabama
Distribution: Fedora / RedHat / SuSE
Posts: 7,155

Rep: Reputation: 56
sed would be a good choice.

sed -i.bak s/oldword/newword/ filename

a backup file is created if you give the extension. The example above would create a backup named filename.bak
 
Old 10-22-2004, 10:16 PM   #3
kvedaa
Member
 
Registered: Mar 2004
Location: Virginia
Distribution: PacketProtector
Posts: 331

Rep: Reputation: 30
One of the great things about linux (& unix) is that their are often several diffrent ways to apprach just about any problem that you are faces with. This is one of those cases. For basic substitution using sed is one of my favorite choices. For instance if I want to replace 'NOT' with 'not' in a file called data I could do something like....

sed 's/NOT/not/' data > new_data; cat new_data > data; rm new_data

as you can see I dump the info out to a temporary holding file called new_data and when it is complete I overwrite the original with the modified version.

Other solutions presented here on linuxquestions include....

http://www.linuxquestions.org/questi...?postid=932751

http://www.linuxquestions.org/questi...hreadid=137374

If you want to do something a bit more complex it might be worth your time to brush up on your perl, which has some very good tools for editing text.

I hope that this helps.
 
Old 10-22-2004, 10:33 PM   #4
darthtux
Senior Member
 
Registered: Dec 2001
Location: 35.7480 N, 95.3690 W
Distribution: Debian, Gentoo, Red Hat, Solaris
Posts: 2,070

Rep: Reputation: 45
For strings more complicated than letters or numbers out might want to check out some sed documentation especially sed and regular expressions.

http://www.hk8.org/old_web/unix/sedawk/index.htm
 
  


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
How to replace a string in a text file jpan Linux - General 3 10-14-2012 07:17 PM
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 Linux - Software 1 07-14-2005 04:22 PM


All times are GMT -5. The time now is 12:29 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration