Add a line to a specific spot in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Add a line to a specific spot in a file
# 1  
Old 12-07-2006
Add a line to a specific spot in a file

Hello,

Simple if you know how, which I don't.

I would like to add a line to file file.env

file.env consists of the following :

line 1-20 here ..
# Begin customizations

ADDED_LINE_TO_GO_HERE

# End customizations
eof

I would like to use a shell script to add the line ADDED_LINE_TO_GO_HERE, but it has to come after # Begin customizations

Any idea how to do this please ?

Cheers,
Dave
# 2  
Old 12-07-2006
Code:
sed "/# Begin customizations/s//&\\
ADDED_LINE_TO_GO_HERE/" file > tmp
mv tmp file

# 3  
Old 12-07-2006
thanks anbu23

how would I deal with something that had $ and / in it please ?
eg
instead of ADDED_LINE_TO_GO_HERE
it was
$ADDED_LINE/to/go/here

I tried the following, but it didn't work ..

sed "/# Begin customizations/s//&\\
$ADDED_LINE/to/go/here/" file > tmp
mv tmp file

it said, sed command was garbled.

Thanks for your time and help, much appreciated.
# 4  
Old 12-07-2006
Code:
sed "/# Begin customizations/s//&\\
\$ADDED_LINE\/to\/go\/here/" file > tmp

# 5  
Old 12-07-2006
Hi Anbu23 and thanks again

so do I have to put a \ in front of $ . ; = , etc ?? I presume this means ignore the fact that it is there and carry on

I have been trying what is below and I get the garbled message again ..

sed "/# Begin customizations/s//&\\
X_TOP=\$A_TOP\/xx\/11.5.0;export X_TOP" a.env > a.tmp

I also tried this and got the same message

sed "/# Begin customizations/s//&\\
X_TOP\=\$A_TOP\/xx\/11\.5\.0\;export X_TOP" a.env > a.tmp

What am I doing wrong please ?

Thanks again.
Dave
# 6  
Old 12-07-2006
Code:
sed "/# Begin customizations/s//&\\
X_TOP=\$A_TOP\/xx\/11.5.0;export X_TOP/" a.env > a.tmp

You missed the forward slash

You have to add \ before special characters like $ / etc
# 7  
Old 12-07-2006
Thanks ! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to update specific value in file with match and add +1 to specific digit

I am trying to use awk to match the NM_ in file with $1 of id which is tab-delimited. The NM_ will always be in the line of file that starts with > and be after the second _. When there is a match between each NM_ and id, then the value of $2 in id is substituted or used to update the NM_. Each NM_... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Add specific string to last field of each line in perl based on value

I am trying to add a condition to the below perl that will capture the GTtag and place a specific string in the last field of each line. The problem is that the GT value used is not right after the tag rather it is a few fields away. The values should always be 0/1 or 1/2 and are in bold in the... (12 Replies)
Discussion started by: cmccabe
12 Replies

3. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies

4. Shell Programming and Scripting

To add a new line with specific text after the pattern is found using sed

hi guys, im trying to add the following line in my xml file <dbrollbacksegs <oa_var="s_db_rollback_segs">NOROLLBACK</dbrollbacksegs> when ever i find the following line <dbsharedpool oa_var="s_dbsharedpool_size">300000000</dbsharedpool> I have succedded till adding a new line... (1 Reply)
Discussion started by: smarlaku
1 Replies

5. Shell Programming and Scripting

Add markup tag and sequential number after specific line

Hello, This one has me a bit stumped. I have data the looks like, M END > <PREDICTION_ACCURACY> PROBABLE > <NO_OF_PARENTS> 3 > <CLOGP> -13.373 > <SMILES> OCC(O)C(OC1OC(CO)C(OC2OC(CO)C > <MIMW> 1006.322419888 (3 Replies)
Discussion started by: LMHmedchem
3 Replies

6. Shell Programming and Scripting

Add new line before specific character

hello all, I have file like this "infile.txt" (all data in one line) A240 582247.99974288.7 8.0 239 582248.19974301.1 8.0 238 582248.39974313.6 8.01A 237 582248.49974326.1 8.0 236 582248.69974338.6 8.0 235 582248.79974351.1 8.01A 234 582248.99974363.5 8.0 233 582249.19974376.0 8.0 232... (4 Replies)
Discussion started by: attila
4 Replies

7. Shell Programming and Scripting

Add sth to end of each line, but only for specific files

Hi. I have a list with files, and I would like to add a variable to the end of each line of each file in this list. The files are in a folder together with a large number of other files which I don't want to change. My code is: for file in 'cat ../list' do sed 's/$/\/R:_0/' $file >>... (4 Replies)
Discussion started by: Bloomy
4 Replies

8. Shell Programming and Scripting

[bash help]Adding multiple lines of text into a specific spot into a text file

I am attempting to insert multiple lines of text into a specific place in a text file based on the lines above or below it. For example, Here is a portion of a zone file. IN NS ns1.domain.tld. IN NS ns2.domain.tld. IN ... (2 Replies)
Discussion started by: cdn_humbucker
2 Replies

9. Shell Programming and Scripting

how to Add word at specific location in line

eg . i have file x.txt contains : java coding , shell scriptting etc... now i want to add "is langauge" after word java. output should be java is langauge coding , shell scriptting etc... any idea how to use shell script to do it ? (10 Replies)
Discussion started by: crackthehit007
10 Replies

10. Shell Programming and Scripting

how to add a line after a specific line

hi I want to add a line just after a specific line can you please help? thx (2 Replies)
Discussion started by: melanie_pfefer
2 Replies
Login or Register to Ask a Question