Perl script for data manipulation

I'm just getting into the specifics of what I need to do, but for right now I would like just basic guidance. I have a fixed width text file from which I need to pull data and append to another fixed width file, with a different schema. What is the basic, perl script structure to pull data in certain positions from the first file and append this data in certain positions to the output file?

For instance, how would I pull the data from positions 9-15 from the source file and place this data in positions 25-31 in the output file? Thanks in advance.
isaacr25Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
ozoConnect With a Mentor Commented:
while( <> ){
printf""%35.7s\n",substr($_,8,7)
}
0
 
isaacr25Author Commented:
ozo,
Where would I specify the source and destination file names?
And how did you come up with the 35.7 and _,8,7?

Excuse my ignorance. I'm not a perl expert, so any clarification would be appreciated.
0
 
ozoConnect With a Mentor Commented:
source is standard input, destination is standard output, so you could call it as

perlscript source > destination
there are 7 characters data in positions 25-31 ending in character 31, so
printf"%31.7s\n"
Sorry I mistyped the first time
substr starts counting at 0, for 7 characters substr($_,8,7)
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Adam314Connect With a Mentor Commented:
With the code ozo gave, yo would specify the filenames on the command line, like:
    Script.pl input > output

The 8,7
    The 8 is the starting character.  The first character is 0, the second 1,...
    The 7 is the number of characters.


The 35.7 is the width used for output.  You can get more info here:
http://perldoc.perl.org/functions/substr.html
http://perldoc.perl.org/functions/sprintf.html

You might also want to look at the unpack function, or use a regular expression:
http://perldoc.perl.org/functions/unpack.html
http://perldoc.perl.org/perlretut.html
0
 
isaacr25Author Commented:
How would I include multiple manipulations?
0
 
ozoCommented:
what manipulations do you want to do?
0
 
isaacr25Author Commented:
I would need to do many more instances of similary manipulations, taking strings from certain source file positions and placing them in specific destination positions.
0
 
isaacr25Author Commented:
I also get the following error when running the script:
Substitution pattern not terminated at e:\testing\datascript.pl line 2.
0
 
Adam314Commented:
You can seperate a line into it's parts using several methods.
One is the substr function ozo showed.  Another is unpack.  Another is regular expressions.

Eg, unpack:
my $str = "aaabbbbbcccccccccc";
my ($a, $b, $c) = unpack("A3A5A10", $str);
#Now
#    $a="aaa"
#    $b="bbbbb"
#    $c="cccccccccc"

eg, regular expression:
my $str = "aaabbbbbcccccccccc";
my ($a, $b, $c) = $str =~ /(.{3})(.{5})(.{10})/;
#Now
#    $a="aaa"
#    $b="bbbbb"
#    $c="cccccccccc"
0
 
ozoCommented:
I mistyped an extra " 
""%35.7s\n"
should have been
"%31.7s\n"
0
 
isaacr25Author Commented:
Concerning the multiple manipulations, here is what I'm trying to do:

while( <> ){
printf"%4.4s\n",substr($_,12,4)
printf"%33.7s\n",substr($_,126,7)
}

And so on. Obviously this is not the correct syntax since I get an error. How should I separate the commands? Thanks.
0
 
ozoConnect With a Mentor Commented:
while( <> ){
printf"%4.4s\n",substr($_,12,4);
printf"%33.7s\n",substr($_,126,7);
}
0
 
isaacr25Author Commented:
Thanks Ozo,
I have another question!

The script is placing each parsed piece on a separate line in the output file. So if I run the above code with two manipulations on a source file that has 3 lines of data, then the output file has 6 lines of data. I need for the parsed pieces in one record to be output to one record in the output file. How do I do this?
0
 
Adam314Connect With a Mentor Commented:
The   \n   in the printf means new line.  So only have the \n on the last printf statement:


while( <> ){
printf"%4.4s",substr($_,12,4);
printf"%33.7s\n",substr($_,126,7);
}
0
 
ozoCommented:
If you want both fields on the same line you might do
while( <> ){
   printf"%4.4s%29.7s\n",unpack"x12A4x110A7",$_;
}
0
All Courses

From novice to tech pro — start learning today.