473,748 Members | 10,058 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,$ye ar) = (localtime(time ))[3,4,5];
$year -= 100 ;
$mon++;
my $date = sprintf("%02d%0 2d%02d%02d","20 ",$year,$mon,$m day);
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)=spl it(" ",$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 12837
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$secon d\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*********@ne tzero.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,$ye ar) = (localtime(time ))[3,4,5];
$year -= 100 ;
$mon++;
my $date = sprintf("%02d%0 2d%02d%02d","20 ",$year,$mon,$m day);
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)=spl it(" ",$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,$n ame) = unpack("A2x19A2 9A29",$_);

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,$n ame) = unpack("A2x2A3x 4A*",$_);

#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(m y $line=<INPUT>)) {
my ($type,$code,$n ame) = unpack("A2x19A2 9A29",$_);
printf OUTPUT "%2s%19s%29s%30 s\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
2768
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", "credits" or "license" for more information. >>> import os >>> file='/TEST/FILE.EXT'
7
11955
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 (monthly). The original data for the table came in the form of a single, large text file, which we imported. This table contains name and address information on potential
6
21893
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 someone please help me. I could do it in access with an update query but things are a little different in SQL server so I am a little lost. Eg. Name John?Doe
3
12377
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 part and a extension part. For example: '/Users/t/web/perl-python/I_Love_You.html' becomes
6
6380
by: Senthil | last post by:
Code ---------------------- string Line = "\"A\",\"B\",\"C\",\"D\""; string Line2 = Line.Replace("\",\"","\"\",\"\""); string CSVColumns = Line2.Split("\",\"".ToCharArray());
7
2379
by: byoxemeek | last post by:
I have an array created from an undelimited text file I receive with a format like: 60190012003010203040506070809101112 60190012004010203040506070809101112 6019001200501020304 60190021998040506070809101112 ...... I need to split these values into array items or dataset columns in a
2
5086
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: 00700400000000 00700400001230 00700400102000 I would like 007004 in one column and the rest of the info in another column. I know in excel I could do text to columns, however the is 20,000 records in access and can not export to excel and split the info. ...
5
2574
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 @columns select @columns gives this string the length of @column is not fixed........we may have n number of columns seperated by ',' but we need to display only first 6 out of them
2
1668
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 problem to me. can someone look at my code and please help me maybe i am missing something. <?php $q=$_POST; $jaar=$_POST; $day=$_POST; //echo $day."<br />";
0
8984
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9238
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.