Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Software
User Name
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.


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

Rep: Reputation: 32
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, 07:34 PM   #2
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, 12:45 AM   #3
Registered: Jul 2003
Distribution: Slackware
Posts: 392

Rep: Reputation: 55
Here's one way:

ed << END
e /etc/conf.modules
alias eth0 e100

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



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 05:52 AM
Scripting question mithereal Linux - General 2 09-21-2005 02:02 AM
Scripting question msandford Linux - Newbie 4 09-05-2005 10:23 AM
Scripting question.... Darklight451 Linux - Newbie 2 09-13-2004 11:29 PM
scripting question gearoid Programming 3 05-26-2004 02:43 PM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:16 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration