472,967 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,967 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 12751
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...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.