Operating System - HP-UX
1821589 Members
3370 Online
109633 Solutions
New Discussion юеВ

print join to a file in Perl

 
SOLVED
Go to solution
Ratzie
Super Advisor

print join to a file in Perl

I have been able to prompt user for data, then use join to add it to the data. But, I need to go further.

First off I need to open the file, right now I just type script.pl

Then I need to take the output of the data, and put it into a file. I have tried opening then print to it, then close the file but it does not work.
Actually I need to do 3 things, print the join, then grep for CUST and send that to a file, grep for TELN send that to a file, and grep -v and send everything that does not have CUST of TELN to another file.

#!/opt/perl/bin/perl
system ("clear"); #Clear the screen
$acode="204";


Enter BLD: $room= ; chomp $room;
print "Enter ROOM:"; $room= ; chomp $room;

$[ = 1; # set array base to 1
$, = ','; # set output field separator
$\ = "\n"; # set output record separator

while() {
chomp;
@a=split(",",$_);
print FH join(",",$acode.$a[0],$bld,$room,$a[2],$a[3],"\n") ;
}
12 REPLIES 12
Rodney Hills
Honored Contributor
Solution

Re: print join to a file in Perl

To seperate output based on content to 3 different files, try the following-

open(INP,"open(OT1,">/tmp/ot1file");
open(OT2,">/tmp/ot2file");
open(OT3,">/tmp/ot3file");
while() {
chomp;
if (/TELN/) { print OT1 $_,"\n"; }
elsif (/CUST/) { print OT2 $_,"\n"; }
else { print OT3 $_,"\n"; }
}

HTH

-- Rod Hills
There be dragons...
H.Merijn Brand (procura
Honored Contributor

Re: print join to a file in Perl

first of all, you'll probably mean

print "Enter BLD >";
chomp ($bld = );

instead of the syntax error over there

Secondly, I'd advice you to NEVER EVER fiddle with $[, which is deprecated already, but will be gone in the next release (5.10)

thirdly, I don't see you make use of the setting of $\

fourth, I think you misunderstand

fifth, you cannot split on ",", you need /,/

--8<---
#!/opt/perl/bin/perl

use strict;
use warnings;

system ("clear"); #Clear the screen
my $acode = 204;

print "Enter BLD >";
chomp (my $bld = );
print "Enter room>";
chomp (my $room = );

open my $fc, ">fileC" or die "fileC: $!";
open my $ft, ">fileT" or die "fileT: $!";
open my $fo, ">fileO" or die "fileO: $!";
while (<>) {
my @a = split /,/, $_, -1;
print join "," => $acode, $a[0], $bld, $room, @a[2,3], "\n";
my $f = /TELN/ ? $ft : /CUST/ : $fc : $fo;
print $f $_;
}
-->8---

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Ratzie
Super Advisor

Re: print join to a file in Perl

Thank you very much for your time, I know that my code was alittle bungled and have made the changes.
I was hoping you can explain the following line to me, I am assuming this is doing a search and placing in the appropriate files, but I get an error when I run it...

my $f = /TELN/ : $ft : /CUST/ : $fc : $fo;
print $f $_;
}

user@server$ ./test.pl FILE
syntax error at ./test.pl line 20, near "/TELN/ :"
Execution of ./test.pl aborted due to compilation errors.

I am assuming that it was a typo with the ? since that did not work either.

Thanks for your help.



Rodney Hills
Honored Contributor

Re: print join to a file in Perl

I think Procura meant-
my $f = /TELN/ : $ft : /CUST/ ? $fc : $fo;

instead of

my $f = /TELN/ : $ft : /CUST/ : $fc : $fo;

The expr ? true : false is kind of a short hand if/then/else, but can be used in variable assignments.

HTH

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: print join to a file in Perl

my $f = /TELN/ ? $ft : /CUST/ ? $fc : $fo;

The first "?" needed to be there too.

-- Rod Hills
There be dragons...
H.Merijn Brand (procura
Honored Contributor

Re: print join to a file in Perl

Ahhhrg, even worse :) ?: (the ternary operator) can be nested

I meant

my $f = /TELN/ ? $ft : /CUST/ ? $fc : $fo;

instead of

my $f = /TELN/ : $ft : /CUST/ : $fc : $fo;

thanks for the catch

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Ratzie
Super Advisor

Re: print join to a file in Perl

To me you guys are god! Thank you.

2 question pop up with this, when it print $f
I get this
,
204xxxx002,Building_d,Room_5,00 0 01 08,TELN NOT

How do I get rid of the leading ,

Second the writing to the files work but it does not place the 204 Building_d or Room_5.
Basically everything from the join. (what you see above is what I need in the file.)

This is the script now...

#!/opt/perl/bin/perl
system ("clear"); #Clear the screen
$acode="204";
print "Enter BLD:"; $bld= ; chomp $bld;
print "Enter OE HOST REMOTE:"; $oe_host_remote= ; chomp $oe_host_remote;


open (INP, "open (TELN, ">/tmp/teln.prtdist");
open (CUST, ">/tmp/cust.prtdist");
open (ERR, ">/tmp/err.prtdist");
#!/opt/perl/bin/perl

use strict;
use warnings;

system ("clear"); #Clear the screen
my $acode = 204;

print "Enter BLD >";
chomp (my $bld = );
print "Enter room>";
chomp (my $room = );

open (inp, "open my $fc, ">fileC" or die "fileC: $!";
open my $ft, ">fileT" or die "fileT: $!";
open my $fo, ">fileO" or die "fileO: $!";
while () {
my @a = split /,/, $_, -1;
print join "," => $acode.$a[0], $bld, $room, $a[1], $a[2], "\n";
my $f = /TELN/ ? $ft : /CUST/? $fc : $fo;
print $f $_;
}

Rodney Hills
Honored Contributor

Re: print join to a file in Perl

Like Nomad from Star Trek, you seem to have melded our 2 samples together...

Is it truly as you show here?

-- Rod Hills
There be dragons...
Ratzie
Super Advisor

Re: print join to a file in Perl

ahhhhhhh sorry for the confusion, I have being playing, and testing and copy paste with out looking gets you into trouble.

Here is what I would like to work with...

#!/opt/perl/bin/perl

use strict;
use warnings;

system ("clear"); #Clear the screen
my $acode = 204;

print "Enter BLD >";
chomp (my $bld = );
print "Enter room>";
chomp (my $room = );

open my $fc, ">fileC" or die "fileC: $!";
open my $ft, ">fileT" or die "fileT: $!";
open my $fo, ">fileO" or die "fileO: $!";

while (<>) {
my @a = split /,/, $_, -1;
print join "," => $acode.$a[0], $bld, $room, $a[1], $a[2], "\n";
my $f = /TELN/ ? $ft : /CUST/? $fc : $fo;
print $f $_;
}

So once again I am trying to figure out how to ...
when it print $f
I get this
,
204xxxx002,Building_d,Room_5,00 0 01 08,TELN NOT

How do I get rid of the leading ,

Second the writing to the files work but it does not place the 204 Building_d or Room_5.
Basically everything from the join. (what you see above is what I need in the file.)
Rodney Hills
Honored Contributor

Re: print join to a file in Perl

Because you are missing a "chomp".

while (<>) {
chomp;
my @a = split /,/, $_, -1;
print join "," => $acode.$a[0], $bld, $room, $a[1], $a[2], "\n";
my $f = /TELN/ ? $ft : /CUST/? $fc : $fo;
print $f $_;
}

In the code above, the original input lines are being sent to the 3 files and the new line displayed to STDOUT. Is that what you want? Or did you want the updated lines printed to the 3 output files?

-- Rod Hills
There be dragons...
Ratzie
Super Advisor

Re: print join to a file in Perl

I wanted the update lines to be printed to the 3 files.

Thanks.
Rodney Hills
Honored Contributor

Re: print join to a file in Perl

Then,

print join "," => $acode.$a[0], $bld, $room, $a[1], $a[2], "\n";
my $f = /TELN/ ? $ft : /CUST/? $fc : $fo;
print $f $_;

should be changed to:
my $f = /TELN/ ? $ft : /CUST/? $fc : $fo;
print $f join "," => $acode.$a[0], $bld, $room, $a[1], $a[2], "\n";

HTH

-- Rod Hills
There be dragons...