cron job to extact lines from files to another file and ftp to new server

i have a text file in this format: which creates a new one everyday in the form of filename _zing__r200_2012_8_10_log.txt

Fri Aug 10 07:29:17 EDT 2012, usera(192.168.0.245) to anotheruser: hey top, this is a private test
Fri Aug 10 07:29:28 EDT 2012, anotheruser(192.168.0.245) to usera: got you top
Fri Aug 10 07:29:33 EDT 2012, anotheruser(192.168.0.245) to usera: returning the test
Fri Aug 10 07:29:58 EDT 2012, anotheruser(192.168.0.245) to usera: this is a test
Fri Aug 10 07:30:05 EDT 2012, anotheruser(192.168.0.245) to all: hey folks
Fri Aug 10 07:30:09 EDT 2012, anotheruser(192.168.0.245) to all: what is going on??
Fri Aug 10 07:30:22 EDT 2012, usera(192.168.0.245) to all: chilling, chilling top
Fri Aug 10 07:30:28 EDT 2012, usera(192.168.0.245) to all: what's up with you  ??
Fri Aug 10 07:30:51 EDT 2012, usera(192.168.0.245) to anotheruser: another tes top before disabling pv
Fri Aug 10 07:30:54 EDT 2012, usera(192.168.0.245) to anotheruser: got me ??
Fri Aug 10 07:31:02 EDT 2012, anotheruser(192.168.0.245) to usera: loud and clear

I would like to run a cron job to extract all the lines wich has the pattern Fri Aug 10 07:30:05 EDT 2012, anotheruser(192.168.0.245) to all: to a new file like 2012_8_10_log.txt which I will either scp or ftp to another server.

my ultimate goal is to take that file, use regualr expression such as

$patterns = array();
$patterns[0] = '/\,/';
$patterns[1] = '/(\(.*\))/';
$patterns[2] = '/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/';
$patterns[3] = '/(?<=all)\:/';

to get each line like this

Wed Jul 04 08:23:55 EDT 2012|~|anotheruser|~| testeeeeeeeeeeeeeeeeettttttttttttttttttttt, this a line test  

and insert in mysql using |~| as delimiter.

any help, tip or pointer ??

thanx

awk understands tokens, which lets you do things on 'the third column' instead of a big regex with width specifiers, so I'd use it here to make things simpler.

awk '{ sub(/,/, "|~|", $6); # Change , to |~| in column 6
                  sub(/[(].*/, "", $7); # Delete everything in column 7 after first (
                  $8=""; # Blank to
                  $9="|~|"; # blank username and replace with |~|
                  }' inputfile > outputfile
1 Like

I'll be sure to use [code] next time

The regex was for the php side. sorry

I do not quite get it, how would awk know the 6th column and on?