apok69 has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks, I've struggling with a script for the past few days. I have report that I need to parse into a CSV file and I have it somewhat working but could use some help making it better. The text file that I need to parse has the following format and is repeated per page:
Per CountZero's suggestion here is a mock up of the text file.I need to combine "text1" with each line of the columns into a CSV record. The most recent thing I found out is that each of the column lines is variable, and there the number of white spaces in between are variable. Here is the script that I have, but I was wondering what I could do to take into variability of the lines. Also, I'm not very knowledgeable about PERL. I've put this together from skimming some books and picking up things on the internet.
The output then would be something like this:
1,TEST,TT,TT00,TT001,NO,xxxx
1,TEST,TT,TT00,**TT00.2,YEST,XXXXXX
#! /usr/bin/perl $OutPut= '>secout.txt'; open(INFILE,'sec_rpt3.txt') or die "Can't open file.\n"; open(OUT, $OutPut) or die "Can't open output.\n"; sub rtrim($) { my $string = shift; $string =~ s/\s+$//; return $string; } sub trim($) { my $string = shift; $string =~ s/^\s+//; $string =~ s/\s+$//; return $string; } sub ltrim{ my $string = $_; $string =~ s/^\s*//; return $string; } while (<INFILE>) { $ThisLine=ltrim($_); chomp($ThisLine); $LineLen=length($ThisLine); if (index($ThisLine,'IMPORTANT TEXT') ne -1) { $LenSec=int($LineLen)-17; $SecClass=substr($ThisLine,17,$LenSec); } if (index($ThisLine,"TEST") ne -1) { $pline = trim(substr($ThisLine,0,16)); $mod = trim(substr($ThisLine,18,6)); $tok = trim(substr($ThisLine,24,10)); $form = trim(substr($ThisLine,34,13)); $sec = trim(substr($ThisLine,47,7)); $unsec =substr($ThisLine,54,21); $secfc = substr($ThisLine,76,21); $rec = join(',',$SecClass,$pline,$mod,$tok,$form,$sec,$unsec,$ +secfc); print OUT "$rec\n"; }; } close(INFILE); close(OUT);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Parsing text file to CSV
by CountZero (Bishop) on Aug 25, 2011 at 19:09 UTC | |
|
Re: Parsing text file to CSV
by CountZero (Bishop) on Aug 25, 2011 at 20:17 UTC | |
|
Re: Parsing text file to CSV
by dwm042 (Priest) on Aug 25, 2011 at 20:30 UTC | |
by apok69 (Initiate) on Aug 25, 2011 at 22:49 UTC | |
|
Re: Parsing text file to CSV
by thewebsi (Scribe) on Sep 13, 2011 at 19:56 UTC |