How to paste lines below certain text (Script)

Hello Guys,

I know the command echo " " > filenamehere ; would erase everything in the document. Can the same be done, to paste words below certain text?

For example, lets say i want to paste a line below the line Server name: in apache.conf . What should be added in the script?

Please show a realistic before and after example. Please also mention what Operating System and version you have and what Shell you prefer.

Allright.

Operating System : ubuntu server 10.04
Shell : Bash

Objective : To add a line at the end of another line.

Example:

I want to add a SCGI Mount Point in apache2.conf. Everynew line will have a new value and it has to be below the previous written line.

This is what you'd see at the end of the apache2.conf file after lines and lines of text :

I want to add SCGIMount /RPC3 127.0.0.1:5001 below the SCGIMount /RPC2 127.0.0.1:5000 line. Then in the future, i might add SCGIMount /RPC4 127.0.0.1:5002 below SCGIMount /RPC3 127.0.0.1:5001 and so on.

The script should ask me for the RPC value and the port value and then paste it in the conf file.

If you want to add the line to the end of the file, then this will do:

echo "SCGIMount /RPC3 127.0.0.1:5001" >> apache2.conf
1 Like

Great. This works well. Thank you.

What about, if i want to replace SCGIMount /RPC3 127.0.0.1:5001 with SCGIMount /RPC2 127.0.0.1:5000 ?

If your sed version supports the -i (in place) option:

sed -i 's!SCGIMount /RPC3 127.0.0.1:5001!SCGIMount /RPC3 127.0.0.1:5000!' apache2.conf

Otherwise:

sed 's!SCGIMount /RPC3 127.0.0.1:5001!SCGIMount /RPC3 127.0.0.1:5000!' apache2.conf > tempfile
mv tempfile apache2.conf
1 Like

Thank you. This works in the terminal perfectly. However, it doesn't seem to work in a bash script. Been messing around with all sorts of way for hours now.

An example :

#!/bin/bash
# Script to add a startup item

username=somethingrandom

cp /etc/init.d/originalfile /etc/init.d/$username
sed 's!changeusername!$username!' /etc/init.d/$username > tempfile
mv tempfile /etc/init.d/$username
chown root:root /etc/init.d/$username
chmod 777 /etc/init.d/$username

I've renamed the file in the script to changeusername so its easier. However it doesn't work with the string $username present. If i were to replace $username with "somethingrandomhere" then it would work. However, i want it to change according to the username that i've given in the script.

(Script has been modified for the thread so it would be less complicated. In the real script, I would be asked to input the username)

sed "s!changeusername!$username!" /etc/init.d/$username

The single tics prevent shell evaluation of variables.

1 Like

Can't believe i couldn't see that.

Thank you very much. I'm new to shell scripting and this has upped my understanding.

I know the best way is to read the manual but my brain doesn't work that way. I like trying things out and sorting through the errors. That is the learning process for me.

Anyways, thank you jin and everyone who has helped me.