I want to insert some parts of my log file to mysql.
this is my log file

195.xx.x.x - - [25/Apr/2017:09:60:xx +0200] "POST /userx/index.php?m=contacts&a=form&...

192.xx.x.x - - [25/Apr/2017:09:45:xx +0200] "POST /usery/index.php?m=customer&doajax=request&action=getContacts...

197.xx.x.x - - [25/Apr/2017:09:20:xx +0200] "GET /userx/index.php?m=meeting&doajax=date&id=3

In my database I have the columns [idLog] [date] [user] [module] [action] [doAjax] [ajaxAction] Where:
25/Apr/2017--> date
userx --> user
m=xxx -->module
a=xxx -->action
idLog is auto increment

So I need to insert of my logfile those informations:
INSERT INTO logs VALUES('null',25/04/2017,usery,contacts,form,null,null)
INSERT INTO logs VALUES('null',25/04/2017,usery,customer,null,request,getContacts)
INSERT INTO logs VALUES('null',25/04/2017,userz,meeting,null,date,null)

I want to insert the date that is on the 4th column and some parts of the request that is 7th column (/userx/index.php?m=meeting&doajax=date&id=3)that are in my date base.
Some ideas?... I tried to create a script bash using awk and sed for to cut some parts of my log file and for to insert to my data base


If the fields position are constant eg : if date always in 2nd Field. Then you can use for loop to process file lines and use awk to get the required field and assign to a variable. Within the loop echo the insert query with variable values of date etc to a file.. and finally execute all insert query in once.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.