LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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
 
LinkBack Search this Thread
Old 04-13-2003, 09:27 AM   #1
gsbarry
Member
 
Registered: Jan 2003
Posts: 42

Rep: Reputation: 15
best method to replace data in a text file?


Dear all,

I think most people may face a problem to write scripts to replace data in the some config files.

For example, I have a file "profile.txt". It contains:

name="John"
phone="123456"

If I want to change the name to "Peter". Now, I only know to use sed.

But I found that sed can't feed the output back to the original file.
And I hope to do the things like searching the name tag and replacing its value.

Which tools can fulfill my hope? or sed can already do this job?
 
Old 04-13-2003, 09:45 AM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 58
Try awk.

Or there is a simpler program called "replace" that comes with mysql that may do the trick.
 
Old 04-13-2003, 12:01 PM   #3
Wynd
Member
 
Registered: Jul 2001
Distribution: Slackware 12
Posts: 511

Rep: Reputation: 31
Couldn't you just do something like

sed s/John/Peter/ profile.txt > profile.txt

?
 
Old 04-13-2003, 12:15 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 21,592
Blog Entries: 47

Rep: Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409
Quote:
Originally posted by Wynd
Couldn't you just do something like

sed s/John/Peter/ profile.txt > profile.txt

?
Did you try doing that yourself? Did it work?
 
Old 04-15-2003, 12:00 AM   #5
gsbarry
Member
 
Registered: Jan 2003
Posts: 42

Original Poster
Rep: Reputation: 15
the replacement function in sed can't fulfill my purpose since the data inside "name=" may be anything.

Did awk can do my job?
could you post a sample for me? since awk is quite complicated to understand. Thanks.
 
Old 04-15-2003, 08:21 AM   #6
arnold
Member
 
Registered: Dec 2002
Posts: 226

Rep: Reputation: 30
google
awk tutorial

I prefer ex with a here script or a command file. Simple and powerful
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find and replace text in multiple file Bad_Bob Linux - Software 9 05-08-2008 02:31 AM
Replace text of unknown content with other text in file brian0918 Programming 15 07-14-2005 09:22 PM
Replace text of unknown content with other text in file brian0918 Linux - Software 1 07-14-2005 03:22 PM
How to replace a string in a text file jpan Linux - General 2 01-11-2005 06:02 PM
replace a string/number in a text file jpan Linux - General 3 10-22-2004 09:33 PM


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