Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 77
  • Last Modified:

Parse .log file for SQL import

Trying to parse/prepare below format so that I can pull it into SQL, I had this topic open under Crystal reports and SQL but I'm still having challenges just importing it into 5 columns. (Text in red font)

C--Users-John-S-Pretorius-Desktop-20.jpg
0
John-S Pretorius
Asked:
John-S Pretorius
  • 6
  • 3
  • 3
  • +1
2 Solutions
 
tel2Commented:
Hi John,

Is there just going to be 1 record?  If not, pls give an example with at least 2 records.

What output from the script would you like, exactly?  Give us an example which is based on the input data example you've supplied.

Will you accept Perl solutions, or Bash, PowerShell, or what?
0
 
John-S PretoriusTechnical Service Manager, Mid-AtlanticAuthor Commented:
Thank you for your feedback, see attached. From my research and other comments it sounds that best approach would be to get it into a readable format for SQL, the first step would be to parse the information from the attached log file into a tab delimited file to look like :- (PowerShell sounds like a great approach but it truly does not matter)

07/29/2016 00:02:03    000022673665    012    550    4095.00

C--Users-John-S-Pretorius-Desktop-Ba.log

My approach/wish list was to have a SSIS package that drops each file directly into sql from the log file but I'm stuck as I'm not familiar with the process. I have to implement the solution/method on the machine/network you use to get the file processed on a daily basis so be mindful of that. Will you be able to handle a directory or should I find a way to copy the single file into a separate folder everyday and delete after your process is complete ?
0
 
Abhimanyu SuriSr Database EngineerCommented:
Here is the Script to convert log files content into desired output.
Assumption : Logfile will have same contents as attached

#######################
# USAGE = ./get_sqldata.sh logfile_name
#!/usr/bin/sh
get_sqldata()
{
cat ${1} |awk 'NF'|while read column1 remainder
do
case $column1 in
[0-9]*)
var_date=`echo ${column1} ${remainder}|cut -d ":" -f1,2,3`
;;
MerchantID)
var_Merchant=`echo ${remainder}|cut -d "-" -f2`
;;
TerminalID)
var_TerminalID=`echo ${remainder}|cut -d "-" -f2`
;;
BatchItemCount)
var_BatchItemCount=`echo ${remainder}|cut -d "-" -f2`
;;
NetBatchTotal)
var_NetBatchTotal=`echo ${remainder}|cut -d "-" -f2`
echo ${var_date} $var_Merchant $var_TerminalID $var_BatchItemCount $var_NetBatchTotal
;;
esac
done
}

get_sqldata $1
############################

Now assuming you have multiple logfiles and you want to parse all of them
To achieve that create a list file with name of all logfiles as mentioned below

:/bash>cat list_file
abc.txt
abc.txt1

Once created execute below

cat list_file |while read line
do
. /bash/get_sqldata.sh $line
done

O/P
07/29/2016 00:02:03 000022673665 012 550 4095.00
07/29/2016 00:02:03 000022673665 012 550 4095.00
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
John-S PretoriusTechnical Service Manager, Mid-AtlanticAuthor Commented:
Thank you - WOW!!

Can I assume I would need an application to run this or will this be a .bat file ?
0
 
Abhimanyu SuriSr Database EngineerCommented:
.bat file will work fine. All you need is shell :)
0
 
John-S PretoriusTechnical Service Manager, Mid-AtlanticAuthor Commented:
I opened Windows PowerShell ISE and also get errors when I run see below :

C--Users-John-S-Pretorius-Desktop-20.jpg
0
 
Abhimanyu SuriSr Database EngineerCommented:
The script provided was for bash/korn shell ( Unix not windows )
0
 
John-S PretoriusTechnical Service Manager, Mid-AtlanticAuthor Commented:
I need windows please.
0
 
tel2Commented:
Hi John,

When I asked this question:
 "Will you accept Perl solutions, or Bash, PowerShell, or what?"
and you responded with this answer:
 "PowerShell sounds like a great approach but it truly does not matter"
then some experts could assume that it truly doesn't matter whether they give you a bash solution (that's a UNIX/Linux Borne Again SHell script, which is very similar to a Korn shell script).  I expect that is what Abhimanyu has concluded, and has wasted his time based on that misinformation.

The fact that one of the topic areas you selected was "Shell Scripting", is also not helpful, because if this is Windows then the only type of shell is Powershell, so you should select the "Powershell" topic area so that you target experts in that, rather than general shell scripting experts, most of whom are probably UNIX/Linux shell script writers.
1
 
John-S PretoriusTechnical Service Manager, Mid-AtlanticAuthor Commented:
My Apologies.
0
 
tel2Commented:
I don't know Powershell, John, but Perl can run on Windows, and I do know Perl.  Do you have Perl installed on the machine that will be running this?  Please be completely sure of your answer before responding, otherwise a lot more time could be wasted going down the wrong path.
0
 
Bill PrewCommented:
Here is a small BAT script that should get you there, give it a try.

@echo off
setlocal EnableDelayedExpansion

set FileIn=in.log
set FileOut=out.txt

if exist "%FileOut%" del "%FileOut%"

for /f "usebackq tokens=1-2 delims=- " %%A in ("%FileIn%") do (
  set Token1=%%A
  if "!Token1:~2,1!" == "/" set DateTime=%%A %%B
  if /i "%%A" == "MerchantID" set MerchantID=%%B
  if /i "%%A" == "TerminalID" set TerminalID=%%B
  if /i "%%A" == "BatchItemCount" set BatchItemCount=%%B
  if /i "%%A" == "NetBatchTotal" (
    set NetBatchTotal=%%B
    echo !DateTime!,!MerchantID!,!TerminalID!,!BatchItemCount!,!NetBatchTotal!>>"%FileOut%"
  )
)

Open in new window

~bp
1
 
John-S PretoriusTechnical Service Manager, Mid-AtlanticAuthor Commented:
Thank you Bill, that worked perfect.

Again I apologize for giving wrong information.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now