# 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>
DocumentRoot /var/www/test1
ServerName test1.local
ServerAlias test1
<Directory "/var/www/test1">
Options All
AllowOverride All
</Directory>
</VirtualHost>


.

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.

0
2019-05-06 01:03:02
Source Share

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.

0
2019-05-08 10:33:57
Source

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
do
ex -s $FILENAME << END_EDITS /^<\/VirtualHost>/ O " capital-o, not zero text to insert goes here . w! q END_EDITS done  . 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. 0 2019-05-08 10:32:32 Source 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;\
done

0
2019-05-08 10:05:18
Source