LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-04-2003, 06:47 PM   #1
glock19
Member
 
Registered: Aug 2001
Distribution: Debian Etch
Posts: 510

Rep: Reputation: 31
Scripting question


I am writing a script.

I would like to know how to replace a line of text in a file, with some new text.

Something like:

echo "alias eth0 e100" >> /etc/conf.modules

But instead of appending at the end of the file, I need it to replace the first line in the file.

Thanks for any help!
 
Old 09-04-2003, 08:34 PM   #2
ranger_nemo
Senior Member
 
Registered: Feb 2003
Location: N'rn WI -- USA
Distribution: Kubuntu 8.04, ClarkConnect 4
Posts: 1,142

Rep: Reputation: 47
If it is the very first line, you can use a combination of wc (word count) and tail.

You would need a temp file. Echo your first line into it.

Use wc -l to count the number of lines in the original file.

Subtract one from that to get a #.

Use tail -n # >> tempfile to cat everything past the first line.

Then, replace the original with the tempfile.

Of course, somebody will prob'ly come up with a single line replacement command using sed or awk or something.
 
Old 09-05-2003, 01:45 AM   #3
lyle_s
Member
 
Registered: Jul 2003
Distribution: Slackware
Posts: 389

Rep: Reputation: 52
Here's one way:

ed << END
e /etc/conf.modules
1c
alias eth0 e100
.
w
q
END

It uses a bash here document and the ed(1) editor.

Lyle
 
  


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
scripting question... Basslord1124 Linux - Networking 2 11-09-2005 06:52 AM
Scripting question mithereal Linux - General 2 09-21-2005 03:02 AM
Scripting question msandford Linux - Newbie 4 09-05-2005 11:23 AM
Scripting question.... Darklight451 Linux - Newbie 2 09-14-2004 12:29 AM
scripting question gearoid Programming 3 05-26-2004 03:43 PM


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