LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Scripting question (https://www.linuxquestions.org/questions/linux-software-2/scripting-question-89348/)

glock19 09-04-2003 05:47 PM

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!

ranger_nemo 09-04-2003 07:34 PM

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.

lyle_s 09-05-2003 12:45 AM

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


All times are GMT -5. The time now is 12:49 AM.