At 12:18 PM -0700 12/23/10, Chris Stinemetz wrote:
Hello,

I hope someone can help me. I am trying to parse data from a txt file and output the results to a new file with timestamp in the name of the file.


Look at the open function for reading existing files and creating new ones: perldoc -f open

open( my $in, '<', $filename) or die("Can't open $filename for reading: $!");
open( my $out, '>', $outputfile) or die("Can't create file $outputfile: $!);

(where $outputfile can contain some sort of timestamp).


The format of the txt file is ";" delimited and is several thousand records in length. Below is an example of the .txt format.


If all fields are delimited with ';' characters, and no field contains a ';' character, you can use a simple split to extract the fields into an array (perldoc -f split):

  while( my $line = <$in> ) {
    chomp($line);
    my @fields = split(/;/,$line);


PACE | EVDOPCMD | 33.0 | 101218 | 07 |
8;1023240136;1218;0;1;00a000001a2bcdc7;0310003147702376;ac016d4a;;;5.6.128.8;0;;;;;43234169;43234349;;;10000;1;1;;0;;19;5.6.128.22;172.30.151.5;304;3;304;3;;;;;15;175;15;175;15;175;1;1798;1251;0;0;2;19;20;;;;;1;1;1;0;128;5.6.128.8;;;;;;;301;5.6.128.8;;;8;304;3;;;;1;43244037;;;1;18;43234169;0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;1;;;;;;43234416;0;0;304;3;21;19;175;15;405;1;1;1;1;0;125;1;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|
8;1023240137;1218;0;1;00a000001db74ace;;ac0174ca;43243423;1678442111;5.6.128.8;1;0;;43242544;43244207;43243423;43243647;;;1000;1;1;;0;;19;5.6.128.26;;372;2;372;2;;43243012;0;43243562;15;175;15;175;15;175;1;;;;;5;48;19;20;49;50;;0;1;2;0;68;5.6.128.8;;;;;;;301;5.6.128.8;;;8;372;2;;;;1;43244207;;;1;18;43243423;0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;1;;;;;;43242544;0;0;372;2;21;19;175;15;177;3;1;0;0;0;68;1;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|43243753;0;0;372;2;21;19;175;15;177;3;1;1;1;0;71;1;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|
8;1023240138;1218;0;1;00a000002017ccdb;0310003147833882;aca344d7;;;5.6.128.13;0;;;;;43234160;43234358;;;10000;1;1;;0;;19;5.6.128.31;172.30.151.5;320;2;320;2;;;;;15;75;15;75;15;75;1;2162;1317;0;0;2;19;20;;;;;1;1;1;0;104;5.6.128.13;;;;;;;306;5.6.128.13;;;8;320;2;;;;1;43244164;;;1;18;43234160;0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;1;;;;;;43234404;0;0;320;2;21;19;75;15;279;6;1;1;1;0;64;1;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|
8;1023240139;1218;0;1;00a1000009237b21;0310003147774000;aca3141e;;;5.6.128.13;0;;;;;43235644;43235820;;;9000;1;1;;0;;19;5.6.128.19;172.30.151.5;502;1;502;1;;;;;15;175;15;175;15;175;1;48;79;0;0;2;19;20;;;;;1;1;1;0;124;5.6.128.13;;;;;;;306;5.6.128.13;;;8;502;1;;;;1;43244194;;;1;18;43235644;0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;1;;;;;;43235887;0;0;502;1;21;19;175;15;27;1;1;1;1;0;127;1;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|

I am very new to perl so any help is appreciated.

Ideally I would like to extract the following columns 0, 5, 44, 31, 32, and give them the following headers indicated below.


Extract the data you want using array slices:

  my @data = @fields[0,5,44,31,32];

or into named variables:

  my( $version, $mtype, $rlptxat, $cell, $sector ) = @fields[0,5,44,31,32];



%fieldmap = (
  "Version" => 0,
  "MType" => 5,
  "RLPtxAT" => 44,
  "Cell" => 31,
  "Sector" => 32,
);

Print the headers (one time):

  print $out "Version;MType;RLPtxAT;Cell;Sector\n";

For each record, print the data to the output file:

  print $out join(';',@data), "\n";

You have not explained where the headers should go, or why there are pipe characters in your input or what they mean, so I have ignored them.

--
Jim Gibson
j...@gibson.org

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to