LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Need Help in Reading a rather complex textile in perl!! (https://www.linuxquestions.org/questions/programming-9/need-help-in-reading-a-rather-complex-textile-in-perl-269948/)

domquem 12-24-2004 11:19 AM

Need Help in Reading a rather complex textile in perl!!
 
Hey,

Im new to perl - 6 months actually!

I need some litlle help in reading a text file in perl then retrieving specific rows and columns from it ;then create a new tab delimited file which i can then open in MS Excel.
The text file TO READ FROM is in the form:

ITEM NUMBER|DESCRIPTION|TOTAL ON HAND|TOTAL ON PO|TOTAL ON S/O
MM0176 WOODEN TRAYS (1,000) 0 0

The new file to be created on running the script should be able to have ONLY the fields:

ITEM NUMER<TAB>TOTAL ON HAND <TAB>TOTAL ON S/O
MM0176 (1,000) 0

You can also see the attached file below.

I can then open the tab delimited file in MS Excel.
Any help greatly appreciated!!

Dominique

Cedrik 12-24-2004 02:18 PM

A quick tip to replace all | with tabs in perl would be :

perl -pi.old -e 's/\|/\t/' /path/to/file
(your original file backup'd as /path/to/file.old)

It is not clear if there is carriage return or end of line after "TOTAL ON S/O " in your example

malone1234 12-24-2004 02:32 PM

Re: Need Help in Reading a rather complex textile in perl!!
 
OK, it looks like you want to read in this information and then omit some of it when creating the Excel file, right? So you have 5 items in each line of the input file and you only want 3 of those items in the output.

So you can do the whole file from the comman line like Cedrik said.

If you want to write a script though, to have a little more control over the output, you'll have to do some parsing, which is pretty simple in perl. The split and join functions are perfect for this.

[source]
# please excuse any syntax errors, perl is not my forte

# open the input and ouput files somewhere
...
while(chomp($line = <FIN>))
{
# parse the line you just read
@keys = split(/|/, $line); # split up the line into strings
# separated by the '|' character

# now the next line holds the values of those items
chomp($line = <FIN>);
@values = split(/ /, $line); # these values are separated by spaces

# maybe create a hash from the keys and values here
...

# write them to the output file
# (or choose which you want to write and which you want to ignore)
for($i = 0; $i < @keys; $i++)
{
# concatenate the keys and values back into strings with a new separator
$newKeys = join(/\t/, @keys);
$newValues = join(/ /, @values);

# print to the output file
print FOUT "$newKeys\n" . "$newValues\n";
}

}
[/source]

domquem 12-29-2004 10:34 AM

Thanks Cedrik for the input.
I was not able to check the forum until today.
Will try that out and update you on the result.

domquem 12-29-2004 10:39 AM

Hey,

Thanks Malone1234, i have been so busy lately,i didnt even have time to check my replies.

Anyhow,you are right i lnly need to get 3 out of the 5 fields of data.

Your sample script will work after i have executed what cedrik has given me above..right??

Thanks...looking fwd to your replies.

DomQuem

domquem 12-29-2004 11:09 AM

Sorry guys i may have misled u abit.
The text fiel doesnt have the |separators i just put them there so u could see how the file is organised.
Actually instead of the | we have spaces so u could replace the | with about 4 spacebars to see what i mean.

i also have just noticed i cannot post the file attachment here for u to view..i think it would be great u view the sample file to have a clear idea.
perhaps i could email u if u want??
Hope am not askin too much..thanks
:)
Item Number|Description|TotalOnhand|TotalOnPO|TotalOnS/O

MM0176|WOODEN TRAYS|(1,000)|0|0 EA
BF/0001|110-0503Front Bearing|3|0|0 EA
BF/0002|Piston|1|0|0| EA
BF/0003|Can Rods|2|0|0| EA
BF/0004|Piston Rings|3|0|0| EA
BF/0005|Valve Plate assy|2|0|0 EA
BF/0007|OIL PUMP|1|0|0| EA
BF/0008|Crue Shaft|1|0|0| EA
BF/0009|Gasket Set|1|0|0 EA

billrhodes 12-29-2004 02:26 PM

Try this
 
That other post was a little too complicated. Try this:

Code:

#!/usr/bin/perl -w

use strict;

# The old file to open
my $oldfile = 'old.txt';

# Write new file here
my $newfile = 'new.txt';

# Open the old file
open(OLD, $oldfile) || die $!;

# Open new file
open(NEW, ">$newfile") || die $!;

# This gets the first, third and fourth items from a row in the old file which
# looks like:
#        MM0176    WOODEN TRAYS    (1,000)    0    0 EA
while (<OLD>) {
        print NEW join("\t", (split(/\s{4,}/))[0,2,3]), "\n";
}

close(NEW);
close(OLD);

Obviously, change all the filenames and such where appropriate. And if you have anything in your old file but lines that look like:

MM0176|WOODEN TRAYS|(1,000)|0|0 EA
BF/0001|110-0503Front Bearing|3|0|0 EA
BF/0002|Piston|1|0|0| EA


Then get rid of them.

domquem 12-30-2004 10:18 AM

Thanks bill rhodes for the excellent input.
I have alredy executed the script,the output is all ok but needs some litlle changes to have the desired fields.

Please see below:

Item Number<TAB>Total on hand<TAB>Total on P/O
MM0176(1,000)<TAB>0
BF/0001 3<TAB>0
BF/0002 1<TAB>0
BF/0003 2<TAB>0
BF/0004 3<TAB>0
BF/0005 2<TAB>0
BF/0007 1<TAB>0
BF/0008 1<TAB>0
BF/0009 1<TAB>0

The desired u\outputs should be:

Item Number<TAB>Total on hand<TAB>Total on P/O

MM0176<TAB>(000')<TAB>(000')
BF/0001 <TAB>3<TAB>0
BF/0002 <TAB>1<TAB>0
BF/0003 <TAB>2<TAB>0
BF/0004 <TAB>3<TAB>0
BF/0005 <TAB>2<TAB>0
BF/0007 <TAB>1<TAB>0
BF/0008 <TAB>1<TAB>0
BF/0009 <TAB>1<TAB>0

domquem 01-03-2005 10:45 AM

This is great billrhodes thanks much.
I hadn't seen the output.Just the perfect solution and with less than 15 LOC's.
Thanks!!
happy New year!!

billrhodes 01-03-2005 01:39 PM

Glad it worked out for ya.

BTW, 15 lines was the long version. That really could have been a command-line-only sort of thing. But that's less than useful next time you have to do this sort of thing...

A happy new year to you as well.


All times are GMT -5. The time now is 05:24 AM.