Saturday, November 27, 2010

Re: edit multiple files under multiple directories/folders

On 11/26/2010 10:43 PM, Kaushal Shriyan wrote:
> On Sat, Nov 27, 2010 at 9:50 AM, Tim Chase<vim@tim.thechases.com> wrote:
>> bash$ for i in `seq 0 15`; do sed -i.bak "s@<port>[^<]*</port>@<port>$((8080+i))</port>@g"
>> /home/kaushal/tomcat${i}/server.xml ; done
>>
>> This does create backups as server.xml.bak (you can omit the "-i.bak" from
>> the sed command if you don't want backups).
>
> Line No 30<Server port="8005" shutdown="SHUTDOWN"> to be changed to<Server
> port="8105" shutdown="SHUTDOWN">
> Line No 94<Connector port="8080" maxHttpHeaderSize="8192" to be changed to
> <Connector port="8081" maxHttpHeaderSize="8192"
> Line No 119<Connector port="8009"
> enableLookups="false" redirectPort="8443" protocol="AJP/1.3"
> /> to be changed to
>
> <Connector port="8109"
> enableLookups="false" redirectPort="8443" protocol="AJP/1.3"
> />
>
> The above server.xml changes relates to tomcat1 viz... 8105,8081 and 8109
> similarly it holds true for the rest of the tomcats
> For example for tomcat2 it would be 8205,8082 and 8209 in server.xml

Ah...with actual data, it's pretty straight-forward. The
structure is the same (do the stuff in a shell "for" loop), but
the sed command gets tweaked. It would look something like (all
on one line, but broken down so you can read it more clearly)

sed -i.bak
-e"30s@port="[0-9]*\"@port=\"$(((i*100)+8005))\"@"
-e"94s@port="[0-9]*\"@port=\"$((i+8080))\"@"
-e"119s@port="[0-9]*\"@port=\"$(((i*100)+8080))\"@"
/home/kaushal/tomcat${i}/server.xml

This assumes the line numbers (30,94,119) don't change, or that
you'd update the script/command with the new line-numbers. You
*can* use relative search-matches, but because two of them start
with "<Connector", the expressions get much longer, because you'd
need to disambiguate them. However, assuming line 119 is really
all one line (with "enableLookups" on it), you could change them
to read something like


-e"/<Server[^>]*shutdown=/s@port="[0-9]*\"@port=\"$(((i*100)+8005))\"@"

-e"/<Connector[^>]*maxHttpHeaderSize/s@port="[0-9]*\"@port=\"$((i+8080))\"@"

-e"/<Connector[^>]*enableLookups>/s@port="[0-9]*\"@port=\"$(((i*100)+8080))\"@"

(changing the hard-coded line-numbers to a search regexp that
will find the lines you want).

-tim

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: