LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-22-2007, 08:42 PM   #1
pinoyskull
Member
 
Registered: Jan 2005
Location: server farm
Distribution: CentOS 5.7
Posts: 59

Rep: Reputation: 15
splitting files


Hi,

I have a vhost files that has like 10 or 15 domains configured, I want to split them per domain, is there a way to do it using a script, bash maybe?
 
Old 10-22-2007, 09:35 PM   #2
yongitz
Member
 
Registered: Nov 2005
Location: Davao City, Philippines
Distribution: RHEL, CentOS, Ubuntu, Mint
Posts: 139

Rep: Reputation: 20
Hi bro! Maybe I can help but first, can you be more specific? give an example data of your input files and your expected data.
 
Old 10-22-2007, 09:41 PM   #3
turbo_spool
LQ Newbie
 
Registered: Oct 2007
Location: Rome, NY
Distribution: Slackware, openSUSE, Fedora Core
Posts: 18

Rep: Reputation: 0
Post

Quote:
Originally Posted by pinoyskull View Post
Hi,

I have a vhost files that has like 10 or 15 domains configured, I want to split them per domain, is there a way to do it using a script, bash maybe?
Thats one of the most common uses of scripting: parsing text. Lately I have used Perl a lot for doing such tasks. Below is a program that opens a file, parses for a prompt, then prints some data from a fictional file.

Code:
#!/usr/bin/perl

my $file = "/location/to/file";

open(FILE,">$file");

while ($line = <FILE>) {
  my $index = index($line,"PROMPT");
  if ($index == 0) {
    $index += 6;
    my $data = substr($line,$index,$LENGTH_OF_DATA);
    print "data from file: $data\n";
  }
}
close(FILE);
Once you learn some simple syntax, you can whip up scripts pretty quick. The above introduces syntax you may have know clue about, but that is a small script that does something like what you need.

Hope that helps.
 
Old 10-22-2007, 09:49 PM   #4
PAix
Member
 
Registered: Jul 2007
Location: United Kingdom, W Mids
Distribution: SUSE 11.0 as of Nov 2008
Posts: 195

Rep: Reputation: 40
What does a vhosts file look like? You limit your potential assistance base to script writers that recognise what it is or can be bothered to go and find out on Google - "your friend".

I think this does what you need, given that the start and end patterns are as stated
<VirtualHost and </VirtualHost

Code:
#!/bin/sh
awk '
 BEGIN { fcount = -1 }
# { print $0 } # just for verification
/<VirtualHost/ { fcount++; thisfile=("DDD" fcount) }
/<VirtualHost/,/<\/VirtualHost/ { print $0 > thisfile  }' < domaindata
I think this should do the job for you. You can easily format the numeric part of the redirect filename if you wish. This will put each record in a file of it's own. Here I have used DDD0 as the first file and DDD1 as the second etc.


PAix

PS sorry, but most all of my awk starts in a shell.

Last edited by PAix; 10-22-2007 at 09:52 PM.
 
Old 10-22-2007, 10:17 PM   #5
pinoyskull
Member
 
Registered: Jan 2005
Location: server farm
Distribution: CentOS 5.7
Posts: 59

Original Poster
Rep: Reputation: 15
sorry for posting incomplete, i actually googled for the said script and found a couple, one in python and one in perl, but thank you for posting your input, turbo spool's input should help me in my quest for learning to script

thanks again guys.
 
Old 10-22-2007, 10:30 PM   #6
turbo_spool
LQ Newbie
 
Registered: Oct 2007
Location: Rome, NY
Distribution: Slackware, openSUSE, Fedora Core
Posts: 18

Rep: Reputation: 0
You're welcome. I have had a Perl book before, but in my experience, for the size of the scripts written, its just easier to use the internet most of the time.
 
  


Reply

Tags
split, vhost


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
splitting files not working... mrroboto Linux - Newbie 3 12-13-2006 03:38 PM
Splitting 1.4 GB avi files morgoth2400 Linux - General 3 12-29-2005 03:36 AM
Splitting XML Files Lanmate Linux - Software 0 11-09-2005 04:24 PM
splitting files columnwise manjushp Programming 3 08-25-2005 04:49 PM
splitting vob files ssfrstlstnm Linux - Software 2 07-10-2005 03:55 PM


All times are GMT -5. The time now is 12:02 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration