473,289 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

Want to split a file into columns

Hi All;

I am fairly new to Perl. I have a file with close to 3000 lines
that I would like to split out in a certain way. I would like to put
the record type starting in column 1 for 2 spaces, the employer code
in column 23 for 29 spaces and employer description in column 53 for
30 spaces. I have tried modifying an existing file with no real
success. I haven't found anything that specifically answers my
question. Any guidance would be appreciated.

My input file would look like this

09 A/B A & B Construction Co.

I'm working on a script so that I don't have to move all of these
manually. The code I am working on is the following. I didn't want to
delete anything, so that is why a good bit of it is commented out.
Thanks in Advance


my ($mday,$mon,$year) = (localtime(time))[3,4,5];
$year -= 100 ;
$mon++;
my $date = sprintf("%02d%02d%02d%02d","20",$year,$mon,$mday);
my @records = ( );
$infile .= '.' . $date;
open(INPUT, $infile) or die "Unable to open $infile: $!\n";
#@lines = <INPUT>; # reads in all lines when called like this
my $inrecord = 0;
my $i = -1; # $i++ the first time will make this 0

while(<INPUT>) {
#($a,$b,$c)=split(" ",$string);
$str=split(" ",split,3);

#my $a = substr(1, 2);
#my $b = substr(23, 29);
#my $c = substr(53, 29);

#print $str[0];
#print " ";
#print $str[1];
#print " ";
#print $str[2];
#print "\n";
}
close(INPUT);
my $record;
open(OFH, '>' . $outfile) or die $!;
for $record (@records) {
# split it up per-line
#for $line (split(/\r?\n/, $record)) {
#my $a = substr($line, 1, 2);
#my $b = substr($line, 23, 29);
#my $c = substr($line, 53, 29);

#print @records;
#print "TESTING!!! \n";
#print $a;
#print " ";
#print $string;
#print " ";
#print $c;
#print $line;
#print "\n";
#print $record;
#print "\n";

#print OFH $record;

#open(OFH, '>' . $outfile) or die $!;

# my $a = substr($a, 1, 2);
# my $b = substr($b, 23, 29);
# my $c = substr($c, 53, 29);

# print the whole $record to the outfile
# print OFH $a,$b,$c;
#}
}
# }
#}
close(OFH);
Jul 19 '05 #1
2 12779
SL_McManus wrote:
Hi All;

I am fairly new to Perl. I have a file with close to 3000 lines
that I would like to split out in a certain way. I would like to put
the record type starting in column 1 for 2 spaces, the employer code
in column 23 for 29 spaces and employer description in column 53 for
30 spaces. I have tried modifying an existing file with no real
success. I haven't found anything that specifically answers my
question. Any guidance would be appreciated.

My input file would look like this

09 A/B A & B Construction Co.

Well, there's more than one way to do it (hey, this is Perl, right? :-))
I usually wimp out with a regular expression something like this

while (<>) {
my ($first, $second, $third) = /^(.{2})(.{29})(.{30})/;
print "$first\n$second\n$third\n\n";
}

HTH

David

--

We come here upon what, in a large proportion of cases, forms the
source of the grossest errors of mankind. Men on a lower level of
understanding, when brought into contact with phenomena of a higher
order, instead of making efforts to understand them, to raise
themselves up to the point of view from which they must look at the
subject, judge it from their lower standpoint, and the less they
understand what they are talking about, the more confidently and
unhesitatingly they pass judgment on it.

-- Leo Tolstoy, "The Kingdom of God is Within You"
Jul 19 '05 #2
In article <b2**************************@posting.google.com >,
SL_McManus <sl*********@netzero.net> wrote:
Hi All;

I am fairly new to Perl. I have a file with close to 3000 lines
that I would like to split out in a certain way. I would like to put
the record type starting in column 1 for 2 spaces, the employer code
in column 23 for 29 spaces and employer description in column 53 for
30 spaces. I have tried modifying an existing file with no real
success. I haven't found anything that specifically answers my
question. Any guidance would be appreciated.

My input file would look like this

09 A/B A & B Construction Co.

I'm working on a script so that I don't have to move all of these
manually. The code I am working on is the following. I didn't want to
delete anything, so that is why a good bit of it is commented out.
Thanks in Advance
In fact, most of it is commented out. You really should only post a
working (syntax-error-free) program so people can answer specific
questions. It is hard to tell if you want guidance on the uncommented
out or the commented out code. I will assume the former.


Add the following to get help from perl:

use strict;
use warnings;
use diagnostics;


my ($mday,$mon,$year) = (localtime(time))[3,4,5];
$year -= 100 ;
$mon++;
my $date = sprintf("%02d%02d%02d%02d","20",$year,$mon,$mday);
my @records = ( );
$infile .= '.' . $date;
You need a 'my' in front of $infile now that you have 'use strict;' in
your program. You haven't initialized $infile previously, so you are
adding ".$date" to an empty string. Is the file you want to read really
named something like '.20031001'?
open(INPUT, $infile) or die "Unable to open $infile: $!\n";
#@lines = <INPUT>; # reads in all lines when called like this
my $inrecord = 0;
my $i = -1; # $i++ the first time will make this 0
You don't seem to use $inrecord or $i below, so these could be removed.

while(<INPUT>) {
#($a,$b,$c)=split(" ",$string);
$str=split(" ",split,3);
This won't work. You are using the function split twice here. The line
you just read is in $_, so you should at least be using split("
",$_,3). Of course, 'split(" ",...' won't work if you have spaces in
your field, which your example does. Also, you are assigning the return
from the split function to a scalar, so you will get only the number of
fields split and not the fields themselves. The split data actually
ends up in the @_ array, but you probably didn't know that (I didn't
until I looked it up).

Look at the unpack function instead, or use the substr calls you have
commented out below. Something like this might work, if your fields are
fixed width in the columns implied by your substr calls, although the
example data line you gave above doesn't match them (untested):

($type,$code,$name) = unpack("A2x19A29A29",$_);

Of course, these seem to be your desired output format, so your likely
input format is something else. According to the sample data, this
should be

($type,$code,$name) = unpack("A2x2A3x4A*",$_);

#my $a = substr(1, 2);
#my $b = substr(23, 29);
#my $c = substr(53, 29);

#print $str[0];
#print " ";
#print $str[1];
#print " ";
#print $str[2];
#print "\n";
}
close(INPUT);
my $record;
open(OFH, '>' . $outfile) or die $!;
You don't have anything in $outfile, so this will not succeed.
for $record (@records) {
You don't have anything in @records, so this will do nothing, but then
the rest of your loop is commented out so will do nothing in any case.
# split it up per-line
#for $line (split(/\r?\n/, $record)) {
#my $a = substr($line, 1, 2);
#my $b = substr($line, 23, 29);
#my $c = substr($line, 53, 29);

#print @records;
#print "TESTING!!! \n";
#print $a;
#print " ";
#print $string;
#print " ";
#print $c;
#print $line;
#print "\n";
#print $record;
#print "\n";

#print OFH $record;

#open(OFH, '>' . $outfile) or die $!;

# my $a = substr($a, 1, 2);
# my $b = substr($b, 23, 29);
# my $c = substr($c, 53, 29);

# print the whole $record to the outfile
# print OFH $a,$b,$c;
#}
}
# }
#}
close(OFH);


I suggest you look at the unpack and printf functions:

perldoc -f unpack
perldoc -f printf

I suggest you open the output file at the same time as the input file,
read each record, extract the fields, and write them to the new file.

Something like this (untested):

#!/opt/perl/bin/perl

use strict;
use warnings;
use diagnostics;

my $infile = 'old.dat';
my $outfile = 'new.dat';
open(INPUT, $infile) or die "Unable to open $infile: $!\n";
open(OUTPUT, $outfile) or die "Unable to open $outfile: $!\n";
while(defined(my $line=<INPUT>)) {
my ($type,$code,$name) = unpack("A2x19A29A29",$_);
printf OUTPUT "%2s%19s%29s%30s\n", $type, '', $code, '', $name;
}
close(INPUT);
close(OUTPUT);
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Martijn Ras | last post by:
Heya folks, I ran into the following problem: When i run this on Windows everything is as expected: C:\>python Python 2.2.3 (#42, May 30 2003, 18:12:08) on win32 Type "help", "copyright",...
7
by: Warren Wright | last post by:
Hello, We maintain a 175 million record database table for our customer. This is an extract of some data collected for them by a third party vendor, who sends us regular updates to that data...
6
by: Prit | last post by:
Hi everyone I guess this should be a simple question for the gurus I have a Data in a column which is to be places in 2 columns instead of one. How do i go about doing it in MS SQL server? Could...
3
by: Xah Lee | last post by:
Split File Fullpath Into Parts Xah Lee, 20051016 Often, we are given a file fullpath and we need to split it into the directory name and file name. The file name is often split into a core...
6
by: Senthil | last post by:
Code ---------------------- string Line = "\"A\",\"B\",\"C\",\"D\""; string Line2 = Line.Replace("\",\"","\"\",\"\""); string CSVColumns = Line2.Split("\",\"".ToCharArray());
7
by: byoxemeek | last post by:
I have an array created from an undelimited text file I receive with a format like: 60190012003010203040506070809101112 60190012004010203040506070809101112 6019001200501020304...
2
by: Dscar | last post by:
Hi, I am a beginner in ACCESS, I've imported data into access, and then realized that I need to split the information in one of my columns into 2 columns. the information looks like this:...
5
by: chinni0719 | last post by:
Hi I need to split the string which looks like n.col1 , b.col1 , n.col2 , b.col2, n.col3 , b.col3 , b.col4 , n.col4 , b.col5,n.col5 n.col1,n.col2 are columns these are present in...
2
by: ogo796 | last post by:
Hi guys am having a problem with a split(),i retrieve line from the text file and i wanna split that line.i manage to split two words but splitting the string enclosed on brackets it seems to be a...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.