Including a line of message to numerous documents

So, I have a number of documents in a directory site, and also I require to insert a line of message right into each of them. They have basically the adhering to layout:

<VirtualHost *:80>
        ServerAdmin [email protected]
        DocumentRoot /var/www/test1
        ServerName test1.local
        ServerAlias test1
        <Directory "/var/www/test1">
                Options All
                AllowOverride All


And also I would certainly such as to insert a line prior to the closing

tag. My first presumption is that I need to have the ability to do this with sed, possibly matching and also changing that tag. I'm mosting likely to start trying this currently, yet if any person has a existing means to do this, I would certainly enjoy to hear it.

2019-05-06 01:03:02
Source Share
Answers: 3

If you do not mind Perl attempt :

perl -pi'*.old' -e 's(</VirtualHost>(Your stuff here\n</VirtualHost>)' myfile

The -i button will certainly conserve your old documents with a .old expansion and also print to the existing one.

2019-05-08 10:33:57

You can additionally make use of "ex-spouse" (command - line vi) if the editing and enhancing you intend to do is also rather made complex. As an example, you just intend to do the insert for on one instance of "".

A covering manuscript similar to this can function :

for FILENAME in *.whatever  # Need a criteria to glob, or a list of names
    ex -s $FILENAME << END_EDITS
O " capital-o, not zero
text to insert goes here

This strategy offers you the benefits of "ex-spouse" : locating an area with elaboarte patterns, and also 'arrow activities'. You can do points like locate a pattern, after that locate the next instance, THEN do the insert. Or you can transform message, as opposed to simply doing inserts. Or you can transform in between arrays. Do not neglect that "ex-spouse" allows you make use of "." as the existing line, so.,/ ^ somepatter/s/blah/ foo/ will certainly function.

2019-05-08 10:32:32

Something similar to this :

sed 's/<\/VirtualHost>/yourlinehere\n<\/VirtualHost>/'

jobs. If you intend to duplicate this command on a great deal of documents you can do something like :

for i in *; do sed -i 's/<\/VirtualHost>/yourlinehere\n<\/VirtualHost>/' "$i"; done

Possibly far better (yet untried) many thanks to @ChrisDown :

find . -type f -print0 | while IFS= read -r -d '' filename;\
do sed -i 's/<\/VirtualHost>/yourlinehere\n<\/VirtualHost>/' $filename;\ 
2019-05-08 10:05:18