LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   loading from text file using column width (https://www.linuxquestions.org/questions/programming-9/loading-from-text-file-using-column-width-111596/)

spyghost 11-02-2003 04:25 PM

loading from text file using column width
 
hi,

is it possible to place data according to the column width?

this is because some of the data that i need to place are strings that have spaces in between. also, some data are not "really" separated by anything! they are placed exactly adjacent to each other...

is there a way to do it in MySQL?

thanks...

david_ross 11-02-2003 04:28 PM

You will need to do it in what ever language you are programming in - you can do it by returning the relevant substrings then remove trailing spaces.

spyghost 11-02-2003 04:38 PM

please eleborate, i am new to databases... i don't get it... :confused:

david_ross 11-02-2003 04:42 PM

It really doesn't have anything to do with databases - you are just manipulating a text string. To give examples we need to know the language you are using and a few example records.

spyghost 11-02-2003 05:17 PM

ok, to start with, what i am doing is a web-based database running under rh 9.0, apache web server, mysql, and php.

here is a sample from the text file that i must load into the database prior to querying

03/09/00 08:01:58 Tayabas TAYABAS RTU Addr: 0x30 Acquisition Module Failed
03/09/00 08:02:00 Gateway GATEWAY RTU Addr: 0x26 Acquisition Module Ok
03/09/00 08:02:02 San Pablo 2 S3 CONCENTRATOR Addr: 0x20 Acquisition Module Ok
03/09/00 08:02:04 San Pablo 2 S3 CONCENTRATOR Addr: 0x31 Acquisition Module Ok
03/09/00 08:02:05 San Pablo 2 S3 CONCENTRATOR Addr: 0x22 Acquisition Module Ok
03/09/00 08:02:10 Gateway GATEWAY RTU Addr: 0x26 Acquisition Module Failed
03/09/00 08:02:17 Tayabas TAYABAS RTU Addr: 0x30 Acquisition Module Ok

here are the fields for this text file:
  • date
  • time
  • location - tayabas, gateway, san pablo 2
  • source - tayabas rtu, gateway rtu, s3 concentrator
  • address - addr: 0x?? acquisition
  • device - module
  • Message Text - ok, failed

david_ross 11-03-2003 01:34 PM

Well I'll assume for the case of the example that the data is in a file called "data.in". Thihs example will just print the data out in a different format but you can use it to put the data into a database if you want:
Code:

<?php

$lines = file("data.in");

foreach ($lines as $line) {
$date = substr($line, 0, 8);
$time = substr($line, 9, 8);
$loca = substr($line, 18, 12);
$sour = substr($line, 31, 15);
$addr = substr($line, 56, 4);
$devi = substr($line, 74, 7);
$mess = substr($line, 82, -1);

print "$date - $time<BR>
Location: $loca<BR>
Source: $sour<BR>
Address: $addr<BR>
Device: $devi<BR>
Message: $mess<BR><BR>";
}

?>



All times are GMT -5. The time now is 07:12 AM.