How To Add Text To Specific File?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How To Add Text To Specific File?
# 1  
Old 02-20-2010
Question How To Add Text To Specific File?

There is file named .htaccess. It is located in many directories on my web server. Instead of manually editing each file to add specific text, i thought there is possibly a command which will allow me to do that.

What i need to be done exactly?

In all .htaccess files i need to add/append the following text:
Code:
RewriteRule . /index.php [L]

The above text needs to be added/appended to this code:
Code:
RewriteEngine On

Important! The text must be added below of the code above. I mean it must start with new line and it must be added before the next code.

Here is how it all must look in the end:
Code:
RewriteEngine On
RewriteRule . /index.php [L] <<< This text must be added in between codes
RewriteCond %{REQUEST_FILENAME}

Was i clear on that? I will really appreciate your help people!
Thanks.
# 2  
Old 02-21-2010
It should be simple with the combination find & sed as.,

Code:
find . -iname 'htaccess' -exec sed -i 's@RewriteEngine On@RewriteEngine On\nRewriteRule . /index.php [L]@' {} \;

Before running the above command,
Code:
$ cat 1/htaccess 
RewriteEngine On
testing line2
$ cat 2/htaccess 
testing line1
RewriteEngine On    
this is line3

After running,
Code:
$ cat 1/htaccess 
RewriteEngine On
RewriteRule . /index.php [L]
testing line2
$ cat 2/htaccess 
testing line1
RewriteEngine On
RewriteRule . /index.php [L]
this is line3

Hope your sed allows inplace editing, else try using perl.
# 3  
Old 02-21-2010
Question

What do the codes below mean? Smilie
How to execute them?

Quote:
Originally Posted by thegeek
Code:
$ cat 1/htaccess 
RewriteEngine On
testing line2
$ cat 2/htaccess 
testing line1
RewriteEngine On    
this is line3

After running,
Code:
$ cat 1/htaccess 
RewriteEngine On
RewriteRule . /index.php [L]
testing line2
$ cat 2/htaccess 
testing line1
RewriteEngine On
RewriteRule . /index.php [L]
this is line3

 
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 add value and text to specific lines

In the awk I have a very large tab-delimeted file that I am trying to extract the DP= value put it in $16 and add specific text to $16 with . (dot) in $11-$15 and $18. Only the lines (there are several) that have the formating below in file will have an empty $16. Other lines will be in a... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. 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

3. Shell Programming and Scripting

Add specific text to columns in file by sed

What is the proper syntax to add specific text to a column in a file? Both the input and output below are tab-delineated. What if there are multiple text/fields, such as /CP&/2 /CM&/3 /AA&/4 Thank you :). sed 's/*/Index&/1' del.txt.hg19_multianno.txt > matrix.del.txt (4 Replies)
Discussion started by: cmccabe
4 Replies

4. 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

5. UNIX for Dummies Questions & Answers

Add strings from one file at the end of specific lines in text file

Hello All, this is my first post so I don't know if I am doing this right. I would like to append entries from a series of strings (contained in a text file) consecutively at the end of specifically labeled lines in another file. As an example: - the file that contains the values to be... (3 Replies)
Discussion started by: gus74
3 Replies

6. Shell Programming and Scripting

Assigning a specific format to a specific column in a text file using awk and printf

Hi, I have the following text file: 8 T1mapping_flip02 ok 128 108 30 1 665000-000008-000001.dcm 9 T1mapping_flip05 ok 128 108 30 1 665000-000009-000001.dcm 10 T1mapping_flip10 ok 128 108 30 1 665000-000010-000001.dcm 11 T1mapping_flip15 ok 128 108 30... (2 Replies)
Discussion started by: goodbenito
2 Replies

7. 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

8. Shell Programming and Scripting

add newline in file after finding specific text

Hi All, I am tring to insert a newline with "/" in a text file whenever there is the text "end;" right now I have inside file: . . end; I want to have: . . end; / I tried doing the following within the file :g/^end;/s//end; \/ / (4 Replies)
Discussion started by: jxh461
4 Replies

9. Shell Programming and Scripting

Adding specific text and spaces to each line in a text file

Hi, I wanted to add specific text to each row in a text file containing three rows. Example: 0 8 7 6 5 5 7 8 9 0 7 9 7 8 9 0 1 2 And I want to add a 21 at the beginning of the first row, and blank spaces at the beginning of the second two rows. To get this: 21 0 8 7 6 5 5 7 8... (4 Replies)
Discussion started by: hertingm
4 Replies

10. UNIX for Dummies Questions & Answers

search and replace a specific text in text file?

I have a text file with following content (3 lines) filename : output.txt first line:12/12/2008 second line:12/12/2008 third line:Y I would like to know how we can replace 'Y' with 'N' in the 3rd line keeping 1st and 2nd lines same as what it was before. I tried using cat output.txt... (4 Replies)
Discussion started by: santosham
4 Replies
Login or Register to Ask a Question