Link to home
Start Free TrialLog in
Avatar of dinhchung82
dinhchung82Flag for Viet Nam

asked on

Read every lines in text file and extract the information!

I have text file attached , my task is read file and extract this information based the fixed length of string on each line ( Specification is attached )and take its fields and input into database.
Would anybody give me the solution or example ?
I appreciate it very much .


spec.bmp
A0001002.txt
Avatar of FishMonger
FishMonger
Flag of United States of America image

What have you tried?

What portion of the task do you not know how to do or is giving you problems?

You tagged both PHP and Perl...which one do you really want?

Is this to be done in a web environment?

If you want to use Perl, you might want to start by reading some of the related documentation.

http://perldoc.perl.org/functions/open.html
http://perldoc.perl.org/functions/close.html
http://perldoc.perl.org/functions/substr.html

http://search.cpan.org/~timb/DBI-1.607/DBI.pm
http://search.cpan.org/~lds/CGI.pm-3.42/CGI.pm
Avatar of dinhchung82

ASKER

Hi FishMonger,
For example , i want to use PHP ( web environment)

i want to user browser the file and my code open it , read each line .
To extract the field 1( PAN) , it is position from 0 to 19 .
To extract the field 2 ( Card_name) , it is position from 20 to 50 .
.....
I must loop for each line and take to each field .
I don't know how to extract it .
If we consider if we usse each field as an element of  array , how to process it .

Thanks for your advise!


My knowledge of php is minimal, so I'll back out and let one of the php experts help you.

You could start by reading some of the php docs

http://us3.php.net/manual/en/function.fopen.php
http://us3.php.net/manual/en/function.fread.php
http://us3.php.net/manual/en/function.substr.php
Hi FishMonger,
Thanks so much for your help!
i shall try with perl first. My target is solveed it  ( perl or php is okie)
My target is parsed this file and new take each field from this file on each line.
Would you give me some example , i shall reference the code
SOLUTION
Avatar of FishMonger
FishMonger
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In PHP, here's something similar you can modify for your needs:
<pre>
<?php
$lines=file('data.txt');
foreach($lines as $line){
  preg_match('/(.{19})(.{30})(.{26})(.{35})(.{05})(.{04})(.{03})(.{01})(.{04})(.{01})(.{04})(.{03})(.{03})(.{04})(.{06})(.{08})(.{100})?(.{100})?(.{100})?/',$line,$fields);
  echo print_r($fields,true);
}
?>

Open in new window

Hi FishMonger,
Thanks for your useful code , i try to use it .

Hi ddrudik ,
your code is so short , it seems be perfect but I use your code, the results is below :


Array
(
)
Array
(
)

Using a long regex like that will work, but you sacrifice readability and maintainability.

The substr function is very fast/efficient and is available in both languages and when assigning the vars, they can be named the same as the db field names, which will increase the readability and maintainability.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Wow, Hielo ,
You are so intelligent , perfect solution !!!!!!!
Very useful ....
Hi Hielo ,
I have a  question , i try to code  and use
  "$lines=file('data.txt')" and substr as FishMonger reommends but
The results have problem that doen't skip over blank lines
Mean that it doesn't count the blank --> parse he wrong text.
Why is it so ?

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi all,
We are successful to extract information but i want to input it into an array and use the array to input to DB but the result is not OK , array doesn't have new value ...
My code is below:
Help me pls!



<pre>
<?php
$fieldLengths=array(19,30,26,35,5,4,3,1,4,1,4,3,3,4,6,8,100,100,100);
$lines=file('C:\way4_03_25_20_16\OWS_Work\Data\Card_Prd\Embs\A0001002.133');
$cards2 = array( "", "","","","","","", "", "","","", "", "", "",
			   "","","","","");
foreach($lines as $line){
	      foreach($fieldLengths as $fieldLength){
		             $i=0;
		             $t=substr($line, 0, $fieldLength);
		             echo $t . "|"; //field/column separator   
					                              $line=substr($line,$fieldLength);
					 $cards2[$i] = $t;
					
					 $i++;
					 
					 
      }
      echo "\n"; //end of record/line
	  print_r($cards2);
	  
 
 
	  
}
 
 
exit;
?>
</pre>
			  
			  
			  
			   
			   
			   
			   
 
 
 
                
                
              
               
			    
			  
			   
			   
			   

Open in new window

Does anyone help me ?
That's hielo's code, but I can try to help.  Please attach to this question a text file to test the code with.
my code is below :

<pre>
<?php
$fieldLengths=array(19,30,26,35,5,4,3,1,4,1,4,3,3,4,6,8,100,100,100);
$lines=file('C:\way4_03_25_20_16\OWS_Work\Data\Card_Prd\Embs\A0001002.133');
$cards2 = array( "", "","","","","","", "", "","","", "", "", "",
                           "","","","","");
foreach($lines as $line){
              foreach($fieldLengths as $fieldLength){
                             $i=0;
                             $t=substr($line, 0, $fieldLength);
                             echo $t . "|"; //field/column separator  
                                                                      $line=substr($line,$fieldLength);
                                         $cards2[$i] = $t;
                                       
                                         $i++;
                                         
                                         
      }
      echo "\n"; //end of record/line
          print_r($cards2);
         
 
 
         
}
 
 
exit;
?>
</pre>
                 
Please use "Attach File" and attach the text file to this question that you are testing with:
C:\way4_03_25_20_16\OWS_Work\Data\Card_Prd\Embs\A0001002.133
Since I'm not a php expert, this may need a slight tweak, but give it a try.

<pre>
<?php
$fieldLengths=array(19,30,26,35,5,4,3,1,4,1,4,3,3,4,6,8,100,100,100);
$lines=file('C:\way4_03_25_20_16\OWS_Work\Data\Card_Prd\Embs\A0001002.133');
$cards2 = array();
 
foreach($lines as $line){
    $i = 0;
    foreach($fieldLengths as $fieldLength){
        cards2[] = substr($line, $i, $fieldLength);
        $i += $fieldLength;
    }
}
print_r($cards2);
 
$sql_values = join(",", $cards2);
print $sql_values;
 
?>
</pre>

Open in new window

hi ddrudik:
this file is attached in the first quesiton, file name is A0001002.txt  ( it is the same file , i use)
Hi FisherMonger,
it is error , i am checking your code , you also use text file from the beginning of question to test
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oops, I forgot to reset the path to the datafile.

change:
$lines=file('A0001002');

to:
$lines=file('C:\way4_03_25_20_16\OWS_Work\Data\Card_Prd\Embs\A0001002.133');
it worked , thanks so much , i can insert into db now :)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks so much for all experts ,  i study a lot from all of you and also solve my problem
Thanks for the question and the points.