Operating System - HP-UX
1753467 Members
5105 Online
108794 Solutions
New Discussion

Re: How to split a large file

 
Hein van den Heuvel
Honored Contributor

Re: How to split a large file

Hmmm... this problem is much similar to:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=654588

Oracle alert files have times stamps like:
Fri Jun 25 14:19:13 2004

I'd use a perl script (let's call it alert-by-month.pl) similar to

if (/^\w+\s+(\w+)\s+\d+\s\d+:/) {
if ($month ne $1) {
$month = $1;
open(M,">alert_$month.log") or die;
}
}
print M;

So it looks for lines starting with a word [the day: \w+] followed by whitespace.
It remembers the next word in $1 [the month: (\w+)]
Then, to be sure it is a date stamp is looks for whitespace, numbers, whitespace, numbers, colon" : [day,hour,":" : \s+\d+\s\d+: ]

When it sees that, it compares the new month (in $1) with the current month.
If it is a new month (like the first line!), then it makes that the current month and opens a new output file.
Finally it prints the line on the current file.



Now exectute with:

perl -n alert-by-month.pl alert

Good luck.

Hein
Kent Ostby
Honored Contributor

Re: How to split a large file

Scott -- using the data provided above, here is a solution.

Create a script file called "do":

echo MONTH $1 > .do1
cat $2 >> .do1
awk -f pull_month.awk < .do1 > .do3
mv .do2 archived_data.log
mv .do3 new_data.log
rm .do1

Create an awk file called pull month :

/^MONTH/ {targetmonth=$2;next;}
$2==targetmonth {print $0 > ".do2";next}
{print $0}

Run it as:

do Jun

Example:

do Jun usek

usek contains:
Fri Jun asdf asdf asdf asdfasdf
Fri Jun asdf asdf asdf asdfasdf
Fri Jul asdf asdf asdf asdfasdf
Fri Jul asdf asdf asdf asdfasdf
Fri Aug asdf asdf asdf asdfasdf
Fri Aug asdf asdf asdf asdfasdf

after the run, archived_data_log contains:

Fri Jun asdf asdf asdf asdfasdf
Fri Jun asdf asdf asdf asdfasdf

after the run, new_data_log contains:
Fri Jul asdf asdf asdf asdfasdf
Fri Jul asdf asdf asdf asdfasdf
Fri Aug asdf asdf asdf asdfasdf
Fri Aug asdf asdf asdf asdfasdf
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"