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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
while( <> ){
printf""%35.7s\n",substr($_,8,7)
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
ozoCommented:
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
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Adam314Commented:
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
ozoCommented:
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
Adam314Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.