LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   rename files by content of file (https://www.linuxquestions.org/questions/linux-newbie-8/rename-files-by-content-of-file-4175601721/)

wademac 03-14-2017 07:48 AM

rename files by content of file
 
Hey All,

I have large apache.conf with 99 VirtualHosts and I want to break them out to single conf files for each VH and rename that new VH conf after the ServerName on line 4 of each file. looking for assistance in the scripting of this action?

Turbocapitalist 03-14-2017 08:11 AM

My approach would be to use perl's Config::ApacheFormat module to loop through your existing configuration file and separate out each virtual host and write them to separate files. ServerName or ServerAlias or another identifier might then be included in the file name.

wademac 03-14-2017 08:26 AM

never used perl's Config::ApacheFormat do you have any reference links so I can read up and learn how to use this?

Turbocapitalist 03-14-2017 08:28 AM

Yes, the manual page linked to in the previous post has a full example. You can get the module from the package libconfig-apacheformat-perl on most Debian-based distros.

wademac 03-14-2017 09:04 AM

I used csplit to break out the file

csplit --digits=2 --quiet --prefix=outfile httpd.conf "/</VirtualHost>/+1" "{*}"

this created outputfile##.conf

then ran this to change the file names

for i in *.conf ; do echo mv $i `egrep -m1 -e 'ServerName *' $i | sed -e 's/ServerName//'`; done

that did it

Turbocapitalist 03-14-2017 09:25 AM

Very nice. I had not seen csplit before. I keep reaching for that perl hammer.

grail 03-14-2017 09:53 AM

Always nice to see a new tool I have not played with :) I am curious how the regular expression knows to skip the second of the three /'s? As with sed, I would have thought it would hit the second
and either throw an issue or potentially match the wrong thing??

Also, assuming the split has worked correctly, why would you need to bail on the first match with grep? Wouldn't there only be a single servername in each file?


All times are GMT -5. The time now is 06:43 PM.