How to paste lines below certain text (Script)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to paste lines below certain text (Script)
# 1  
Old 01-21-2012
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?
# 2  
Old 01-21-2012
Please show a realistic before and after example. Please also mention what Operating System and version you have and what Shell you prefer.
# 3  
Old 01-21-2012
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 :

Quote:
...
...
...
...

# Include the virtual host configurations:
Include /etc/apache2/sites-enabled/

SCGIMount /RPC2 127.0.0.1:5000
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.
# 4  
Old 01-21-2012
If you want to add the line to the end of the file, then this will do:
Code:
echo "SCGIMount /RPC3 127.0.0.1:5001" >> apache2.conf

This User Gave Thanks to bartus11 For This Post:
# 5  
Old 01-21-2012
Quote:
Originally Posted by bartus11
If you want to add the line to the end of the file, then this will do:
Code:
echo "SCGIMount /RPC3 127.0.0.1:5001" >> apache2.conf

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 ?
# 6  
Old 01-22-2012
Quote:
Originally Posted by xxxx
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:
Code:
sed -i 's!SCGIMount /RPC3 127.0.0.1:5001!SCGIMount /RPC3 127.0.0.1:5000!' apache2.conf

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

This User Gave Thanks to Franklin52 For This Post:
# 7  
Old 01-22-2012
Quote:
Originally Posted by Franklin52
If your sed version supports the -i (in place) option:
Code:
sed -i 's!SCGIMount /RPC3 127.0.0.1:5001!SCGIMount /RPC3 127.0.0.1:5000!' apache2.conf

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


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 :

Code:
#!/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)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash copy and paste text in file from one position to another

Hi I have a text file with lines beginning with 71303, 71403, 71602, I need to copy the 10 digit text at position 30 on lines beginning with 71303 (5500011446) to position 99 on every line beginning with 71602 (see example below), There may be many 71303 lines but I need the text copying to... (2 Replies)
Discussion started by: firefox2k2
2 Replies

2. Shell Programming and Scripting

Copy and paste text inside a xml file

I have a really big XML file. I need copy the value of one tag inside another one tag. I try to publish one example. <channel update="i" site="merge-xmltv" site_id="" xmltv_id="Rai 1">Rai 1</channel> <channel update="i" site="merge-xmltv" site_id="" xmltv_id="Rai 1 +2HD">Rai 1... (6 Replies)
Discussion started by: Tapiocapioca
6 Replies

3. UNIX for Dummies Questions & Answers

Copy Lines between Keywords & paste them to another file

hi, I have Multiple files with the following data : File1 100414 DR1 END XXXXX Test1 Test2 Test3 Test4 Test5 Test6 END 100514 DR2 END XXXXX Test7 Test8 Test9 Test10 Test11 Test12 END 100614 DR3 (5 Replies)
Discussion started by: newageBATMAN
5 Replies

4. OS X (Apple)

Paste text into vi file?

Hello, I'm running OS X 10.7.4. How can I paste text from the pasteboard into an open vim file? Thanks! (21 Replies)
Discussion started by: palex
21 Replies

5. Solaris

Copy and paste text from a word document into a txt file in vi

Hello, Can anybody please tell me how we can copy and paste text from a word document into a text file that we are editing in vi? Is it possible to do that while we are editing the text file in vi in insert mode? Thanks, (3 Replies)
Discussion started by: Pouchie1
3 Replies

6. UNIX for Dummies Questions & Answers

copy and paste certain many lines of huge file in linux

Dear All, I am working with windoes OS but remote a linux machine. I wonder the way to copy an paste some part of a huge file in linux machine. the contain of file like as follow: ... dump annealling all custom 10 anneal_*.dat id type x y z q timestep 0.02 run 200000 Memory... (2 Replies)
Discussion started by: ariesto
2 Replies

7. Shell Programming and Scripting

awk/grep copy and paste and insert in between lines.

Hi all, I'm a unix newb andI'm trying to write a script that can copy some text paste it in a certian place and then add a number. It's not really clear but I'll show an example. what the file looks like right now: Linux 2.6.24-24-generic (abc) 07/15/09 23:25:01 CPU ... (6 Replies)
Discussion started by: the1hand3r
6 Replies

8. Shell Programming and Scripting

Need helpl in scripting read 2 lines and paste to one single line.

Hi All, I need to create script which will accept one file as i/p and give me o/p file described as below. 1) i/p log file named abc.log contents several lines but i am interested in lines like below. #FILE..... /oracle/XYZ/sapdata1/undo_7/undo.data7 #SAVED.... BACKINTID001 2) o/p... (4 Replies)
Discussion started by: paragp1981
4 Replies

9. UNIX for Dummies Questions & Answers

vi editor prefixes lines with # upon paste

I've been away from Unix and the vi editor for a while, and now I'm using vi (actually vim) in a Cygwin bash shell. When I copy-and-paste code examples (I'm playing with perl now) any time I paste code with lines beginning with the # character, vi inserts a # character at the beginning of every... (2 Replies)
Discussion started by: greenmangroup
2 Replies

10. UNIX for Dummies Questions & Answers

Copy/Paste text as commands in AIX

Hello, I'm absolutely new to this world... but I've a problem with a terminal connected via PuTTY (or Termlite) to an AIX 5.1 application. The problem: I need to paste from clipboard a text containing both input text strings and special keys as ESC, Arrows and so on, to execute in the AIX... (1 Reply)
Discussion started by: Daniele11
1 Replies
Login or Register to Ask a Question