LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-23-2008, 03:47 PM   #1
jeriryan
Member
 
Registered: Apr 2003
Location: United States
Distribution: RHEL 5.4, Snow Leopard
Posts: 87

Rep: Reputation: 15
Replacing a line in a text file


Is there a way to change one line in a text file from the command line? For example, if inside a config file there is a line that says startup=manual and I want it to say startup=automatic, how can I accomplish this from the command line? Also, there will definitely be multiple instances of the words "startup", "manual", and "automatic" in the file, just not in that particular order, and I can only modify that one line.
 
Old 06-23-2008, 04:20 PM   #2
zephyrcat
Member
 
Registered: Jun 2007
Posts: 107

Rep: Reputation: 15
Well, you could just open the file and search for the line you want to change.

If you want to do that, you can use the terminal to open the file in gedit or vim or whatever.
Code:
gedit filename
Or look here for information about Vim:
http://www.linuxquestions.org/linux/...IM_Vi_Improved

Hope that helps!
 
Old 06-23-2008, 04:27 PM   #3
jeriryan
Member
 
Registered: Apr 2003
Location: United States
Distribution: RHEL 5.4, Snow Leopard
Posts: 87

Original Poster
Rep: Reputation: 15
I can't open the file to edit though (I don't mean it isn't possible, but the requirement is command line only.) Is that possible?
 
Old 06-23-2008, 04:28 PM   #4
LiamFromLeeds
Member
 
Registered: Dec 2006
Distribution: Arch
Posts: 44

Rep: Reputation: 15
If you don't want to open the file then sed is your friend.

Code:
sed -e 's/startup=manual/startup=automatic/' fileName > fileName.tmp
You can then check your changes in fileName.tmp if they are OK then overwrite the new file back on top of the old one..

Code:
mv fileName.tmp fileName
 
Old 06-23-2008, 04:34 PM   #5
zephyrcat
Member
 
Registered: Jun 2007
Posts: 107

Rep: Reputation: 15
Looks like LiamFromLeeds has a good solution. I just wanted to note that if the just can't use a GUI program to edit the file, you could still use a program like vim, which runs in a CLI.
 
Old 06-23-2008, 04:56 PM   #6
jeriryan
Member
 
Registered: Apr 2003
Location: United States
Distribution: RHEL 5.4, Snow Leopard
Posts: 87

Original Poster
Rep: Reputation: 15
Thanks guys, it worked perfectly.
 
  


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
Replacing words in a text file Raghavan_sat Programming 3 05-27-2008 04:11 PM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 09:22 AM
replacing text of a file on remote system bajaj111 Linux - Software 4 11-07-2006 02:43 AM
replacing text in file kushalkoolwal Programming 17 10-25-2005 06:25 AM


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