Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

regex & mysql & perl

Posted on 2013-12-28
2
Medium Priority
?
322 Views
Last Modified: 2014-01-07
I have some files as given below and it is generated by an other software i need to record them into mysql with

date cabinet port upload download time

columns


K16.Result.28-12-2013-11-08-58.txt

cabinet : K16
time : 28-12-2013 11:08:58

from the file
2 1048941172 7134284

port  : 2
upload : 1048941172
download  : 7134284

[root@sflow snmpactivity]# ls -l
total 20
-rw-r--r-- 1 root root 1044 Dec 28 11:09 K16.Result.28-12-2013-11-08-58.txt
-rw-r--r-- 1 root root  744 Dec 28 11:09 K17.Result.28-12-2013-11-08-58.txt
-rw-r--r-- 1 root root 1247 Dec 28 11:09 ROUTER.Result.28-12-2013-11-08-58.txt
-rw-r--r-- 1 root root 2475 Dec 28 11:09 T01.OUT.28-12-2013-11-08-58.txt
-rw-r--r-- 1 root root  925 Dec 28 11:09 T01.Result.28-12-2013-11-08-58.txt

Open in new window



[root@sflow scripts]# cat snmpactivity/K16.Result.28-12-2013-11-08-58.txt
1 0
2 1048941172 7134284
3 3529411188 3185307262
4 1048687588 6735180
5 2840247361 4175184636
6 2657702532 271368251
7 2961612083 1091749773
8 3878772277 2619036428
9 1044640118 188432
10 0
11 3883362145 2744237398
12 4137364222 4011646502
13 3204266302 1070142078
14 0
15 3214335177 3502388384
16 0
17 2325405345 1347644254
18 249701586 2938848862
19 1617958588 2135678611
20 1042992729 2851911
21 3632181468 1141920086
22 2092103702 2803266339
23 1544137143 3535215114
24 1614928891 959086658
25 1049028038 7423758
26 511661802 2644622558
27 1044508473 12530
28 1044518637 19130
29 0
30 1044695107 327690
31 1282656802 3355194564
32 1715099776 1404745494
33 3872591119 4290906417
34 0
35 2183833671 972684281
36 4254282367 2838704160
37 1044668906 3194863
38 1049459257 7695900
39 1048022231 6003614
40 1048261461 5846452
41 3124176458 889028303
42 1852391274 3095560750
43 732955858 2190609680
44 1086479583 67235631
45 1051889807 514447884
46 0
47 0
48 2829606450 2505355220
1000 0
1001 0
1002 0
1003 0
1004 0
1005 0
1006 0
1007 0
100000 0

Open in new window

0
Comment
Question by:3XLcom
2 Comments
 
LVL 27

Accepted Solution

by:
wilcoxon earned 2000 total points
ID: 39744075
This should do it.  You were a little unclear in defining date and time - your desired columns list both but your breakdown of the filename only lists a single datetime called time.
use strict;
use warnings;
use DBI;
# you'll need to define the variables used in the connection string (or replace with literals
my $dbh = DBI->connect("DBI:mysql:database=$database;host=$hostname;port=$port", $user, $passwd);
foreach my $fil (@ARGV) {
    unless ($fil =~ m{^([^.]+\.(\d+-\d+-\d+)-(\d+-\d+-\d+)\.txt$}) {
        warn "file name does not conform - skipping $fil\n";
        next;
    }
    my ($cab, $dt, $tm) = ($1, $2, $3);
    $tm =~ s{-}{:}g;
    open IN, $fil or die "could not open $fil: $!";
    while (<IN>) {
        chomp;
        my ($port, $up, $dn) = split;
        $dn = 0 unless defined($dn);
        $dbh->do("insert into table values (?, ?, ?, ?, ?, ?)", $cab, $dt, $port, $up, $dn, $tm);
    }
    close IN;
}

Open in new window

0
 

Author Closing Comment

by:3XLcom
ID: 39762532
sorry thank you
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Six Sigma Control Plans
Suggested Courses

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question