Need Help in Reading a rather complex textile in perl!!
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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!!
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";
}
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
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
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
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.
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...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.