LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-24-2004, 11:19 AM   #1
domquem
LQ Newbie
 
Registered: Jul 2004
Location: WASHINGTON,DC AREA
Distribution: SUSE LINUX 9.1
Posts: 17

Rep: Reputation: 0
Question 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
 
Old 12-24-2004, 02:18 PM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
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
 
Old 12-24-2004, 02:32 PM   #3
malone1234
LQ Newbie
 
Registered: Nov 2004
Posts: 5

Rep: Reputation: 0
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]
 
Old 12-29-2004, 10:34 AM   #4
domquem
LQ Newbie
 
Registered: Jul 2004
Location: WASHINGTON,DC AREA
Distribution: SUSE LINUX 9.1
Posts: 17

Original Poster
Rep: Reputation: 0
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.
 
Old 12-29-2004, 10:39 AM   #5
domquem
LQ Newbie
 
Registered: Jul 2004
Location: WASHINGTON,DC AREA
Distribution: SUSE LINUX 9.1
Posts: 17

Original Poster
Rep: Reputation: 0
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
 
Old 12-29-2004, 11:09 AM   #6
domquem
LQ Newbie
 
Registered: Jul 2004
Location: WASHINGTON,DC AREA
Distribution: SUSE LINUX 9.1
Posts: 17

Original Poster
Rep: Reputation: 0
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
 
Old 12-29-2004, 02:26 PM   #7
billrhodes
LQ Newbie
 
Registered: Dec 2004
Posts: 2

Rep: Reputation: 0
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.
 
Old 12-30-2004, 10:18 AM   #8
domquem
LQ Newbie
 
Registered: Jul 2004
Location: WASHINGTON,DC AREA
Distribution: SUSE LINUX 9.1
Posts: 17

Original Poster
Rep: Reputation: 0
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
 
Old 01-03-2005, 10:45 AM   #9
domquem
LQ Newbie
 
Registered: Jul 2004
Location: WASHINGTON,DC AREA
Distribution: SUSE LINUX 9.1
Posts: 17

Original Poster
Rep: Reputation: 0
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!!
 
Old 01-03-2005, 01:39 PM   #10
billrhodes
LQ Newbie
 
Registered: Dec 2004
Posts: 2

Rep: Reputation: 0
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading Berkeley DB 1.85 in perl Gardener Programming 6 01-28-2015 06:16 PM
Mysql reading via Perl DBI ivanatora Programming 2 03-21-2007 04:03 AM
Perl - Reading Real Time streams vitoal18t Programming 10 05-18-2005 06:59 AM
Perl of Wisdom needed for reading in iptables values pjcp64 Programming 5 06-02-2003 05:52 AM
perl reading pdf,ps,txt j-ray Programming 1 02-04-2003 10:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration