Topic: Adding multiple lines into file

hi all,

I'm trying to figure out how to write multiple lines at the end of a file.. reason why I need this is I'm hosting couple of website on my machine and it gets boring to write every time same lines

<VirtualHost yourhostname:80>
    ServerAdmin you@your-domain.com
    ServerName your_ip_address
    DocumentRoot /www
    ErrorLog logs/error_log
    CustomLog logs/access_log combined
</VirtualHost>

Is there a way to write a shell script that will insert these lines at the end of the file... any hints indications how to do it ? Thank you in advance ..

Re: Adding multiple lines into file

Sure, it's very easy:

echo "<VirtualHost yourhostname:80>
    ServerAdmin you@your-domain.com
    ServerName your_ip_address
    DocumentRoot /www
    ErrorLog logs/error_log
    CustomLog logs/access_log combined
</VirtualHost>" >> httpd.conf

The >> operator is the append operator; it works the same as > except that it appends instead of overwrites.

Re: Adding multiple lines into file

Hi
I've got some more problems with editing a big configuration file .. its about 2k lines long.. anyway what I need is to place certain text to certain line number.. lets say I need to place "Something" on line 980 .. "something" else on line number 1500 and so on without tempering the rest of the configuration file ... I found somewhere this

head -981 conf.temp > conf.conf; echo "Email myemail@email.com" >> conf.conf; sed '4,$!d' <conf.temp >> conf.conf

it seems to work on one occasion but when I want to make another change in the configuration whole thing comes messed up, things appear double and so on ... any help ?

Thank you in advance ...

Re: Adding multiple lines into file

For this task, I'd recommend using sed. First, always create a temp file, and use that as input file; never use the same file as input and output file simultaneously. I'll assume now that conf.temp is a copy of conf.conf. To insert a line after line 980, you can use:

sed -e '980a\
Email foo@example.org' conf.temp > conf.conf

Of course, if you run this a second time, then the Email line already exists, so changing it instead of adding a second Email line might be required:

sed -e '981s/^Email .*/Email bar@example.org/' conf.temp > conf.conf

Re: Adding multiple lines into file

It works thank you .. but I have to alter or enter several lines ... after I input 1 line it works perfectly .. but after I input the next one the previous line disappears  ..

Re: Adding multiple lines into file

The first variant shown above will not remove anything, but keep adding lines.

Re: Adding multiple lines into file

Thank you for your posting it works ... how about if I want to use variables inside this .. ex :

serverip=$(awk 'NR==3 {print substr($0,1,14)}' /etc/hosts)
sed -e '1005a\
Server Ip:${serverip}' conf.temp > conf.conf

I can't pass the value of server ip inside sed.. is it possible at all to do this some other way if I'm mistaken.. thanx

Re: Adding multiple lines into file

It works if you use " " to quote your strings instead of ' '.