LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-14-2017, 08:48 AM   #1
wademac
Member
 
Registered: Apr 2008
Posts: 43

Rep: Reputation: 19
Question 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?
 
Old 03-14-2017, 09:11 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,091
Blog Entries: 3

Rep: Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665
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.
 
1 members found this post helpful.
Old 03-14-2017, 09:26 AM   #3
wademac
Member
 
Registered: Apr 2008
Posts: 43

Original Poster
Rep: Reputation: 19
never used perl's Config::ApacheFormat do you have any reference links so I can read up and learn how to use this?
 
Old 03-14-2017, 09:28 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,091
Blog Entries: 3

Rep: Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665
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.
 
1 members found this post helpful.
Old 03-14-2017, 10:04 AM   #5
wademac
Member
 
Registered: Apr 2008
Posts: 43

Original Poster
Rep: Reputation: 19
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
 
4 members found this post helpful.
Old 03-14-2017, 10:25 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,091
Blog Entries: 3

Rep: Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665
Very nice. I had not seen csplit before. I keep reaching for that perl hammer.
 
Old 03-14-2017, 10:53 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,988

Rep: Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182
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?

Last edited by grail; 03-14-2017 at 10:55 AM.
 
  


Reply

Tags
apache2conf, bash, scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] rename files after reading source file tofayel001 Linux - Newbie 14 08-07-2014 08:26 AM
Rename files using csv file and awk tuxlux Programming 6 09-03-2013 08:46 PM
[SOLVED] How to rename a part of a file or files annonyxxxx Linux - Newbie 5 05-14-2013 04:45 AM
bash-code to rename files based on config file Yalla-One Programming 20 04-14-2010 02:39 PM
Rename 100+ files from a text file AGazzaz Linux - Software 9 09-26-2008 01:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration