Parsing a delimited text file into mysql database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • s4ndr0ck
    New Member
    • Feb 2008
    • 3

    Parsing a delimited text file into mysql database

    Alrights....the delimited text in the topic is like this :

    (Btop - 19:37:12 up 9:43, 1 user, load average: 0.20, 0.10, 0.03(B
    Tasks:(B(B 105 (Btotal,(B (B 1 (Brunning, (B(B 104 (Bsleeping, (B(B 0 (Bstopped, (B(B 0 (Bzombie(B 
    Cpu(s):(B(B 1.4%(Bus,(B(B 1.6%(Bsy,(B(B 0.0%(Bni,(B(B 96.3%(Bid,(B(B 0.4%(Bwa,(B(B 0.3%(Bhi,(B(B 0.1%(Bsi,(B(B 0.0%(Bst(B
    Mem: (B(B 515224k (Btotal,(B (B 486332k (Bused,(B(B 28892k (Bfree,(B(B 71916k (Bbuffers( B
    Swap:(B(B 1048568k (Btotal,(B (B 0k (Bused,(B(B 1048568k (Bfree,(B(B 270032k (Bcached(B 
    
     PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND (B
    (B 2264 root 15 0 38936 13m 6664 S 3.8 2.6 9:04.22 Xorg (B
    (B(B 6206 root 15 0 2164 928 716 R 3.8 0.2 0:00.04 top (B
    (B 6123 root 15 0 39976 11m 8312 S 1.9 2.3 0:00.78 gnome-terminal (B
    (B 6204 root 17 0 4436 1020 904 S 1.9 0.2 0:00.01 autoscript (B
    (B 1 root 15 0 2032 628 540 S 0.0 0.1 0:03.93 init (B
    (B 2 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/0 (B
    (B 3 root 34 19 0 0 0 S 0.0 0.0 0:00.01 ksoftirqd/0 (B
    (B 4 root RT 0 0 0 0 S 0.0 0.0 0:00.02 watchdog/0 (B
    (B 5 root 10 -5 0 0 0 S 0.0 0.0 0:00.73 events/0 (B
    (B 6 root 10 -5 0 0 0 S 0.0 0.0 0:00.01 khelper (B
    (B 7 root 10 -5 0 0 0 S 0.0 0.0 0:00.00 kthread (B
    (B 10 root 10 -5 0 0 0 S 0.0 0.0 0:00.14 kblockd/0 (B
    (B 11 root 20 -5 0 0 0 S 0.0 0.0 0:00.00 kacpid (B
    (B 65 root 17 -5 0 0 0 S 0.0 0.0 0:00.00 cqueue/0 (B
    (B 68 root 10 -5 0 0 0 S 0.0 0.0 0:00.00 khubd (B
    (B 70 root 10 -5 0 0 0 S 0.0 0.0 0:00.03 kseriod (B
    (B 124 root 21 0 0 0 0 S 0.0 0.0 0:00.00 pdflush (B[?12l[?25h
    I would like to get the string with % in the line start with CPU(s) (shown Bold) and put them into a variables..

    Would someone be kind enough to write a php file which will do that?
    Because i know nothing about this parsing things, and just a very little knowledge about php. So the code would need to be complete i hope..
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Originally posted by s4ndr0ck
    Would someone be kind enough to write a php file which will do that?
    Because i know nothing about this parsing things, and just a very little knowledge about php. So the code would need to be complete i hope..
    At this forum we do not provide complete code by request!

    This is a site where experts and programmers help other programmers with the code they have developed themselves or with problems they encounter while developing code.

    For a complete php script you'll have to turn to a paid php programmer, not to this forum.

    Ronald

    Comment

    • s4ndr0ck
      New Member
      • Feb 2008
      • 3

      #3
      I'm sorry...

      I have this line from a text file:
      Code:
      Cpu(s):(B(B 1.4%(Bus,(B(B 1.6%(Bsy,(B(B 0.0%(Bni,(B(B 96.3%(Bid,(B(B 0.4%(Bwa,(B(B 0.3%(Bhi,(B(B 0.1%(Bsi,(B(B 0.0%(Bst(B
      and i have made a code to search this line and put into an array, the code is like this :

      [PHP]
      <?
      $lines = file('cpu050420 081937.txt');

      $teams = array();

      foreach($lines as $line){
      if(preg_match('/Cpu\.\S$/',$line)){
      $teams[] = $line;
      }
      }
      ?> [/PHP]
      I need to find a line started with Cpu(s).

      But it always return an empty array. Could somebody please help me to make it work??

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Using == will work quicker, as in[php]foreach($lines as $line){
        if (substr($line, 0, 3) == 'Cpu')
        $teams[] = $line;
        }[/php]Ronald

        Comment

        • s4ndr0ck
          New Member
          • Feb 2008
          • 3

          #5
          please correct me if i'm wrong,
          [PHP]<? substr($line,0, 3) == Cpu ?>[/PHP]
          is to read $line from 1st character to 3rd character, and compare it to string 'Cpu'

          It works, thank you...

          Comment

          Working...