- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- print join to a file in Perl
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2004 04:33 AM
тАО07-27-2004 04:33 AM
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=
print "Enter ROOM:"; $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") ;
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2004 04:47 AM
тАО07-27-2004 04:47 AM
Solutionopen(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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2004 05:06 AM
тАО07-27-2004 05:06 AM
Re: print join to a file in Perl
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2004 05:56 AM
тАО07-27-2004 05:56 AM
Re: print join to a file in Perl
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2004 06:18 AM
тАО07-27-2004 06:18 AM
Re: print join to a file in Perl
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2004 06:24 AM
тАО07-27-2004 06:24 AM
Re: print join to a file in Perl
The first "?" needed to be there too.
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2004 06:24 AM
тАО07-27-2004 06:24 AM
Re: print join to a file in Perl
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2004 07:07 AM
тАО07-27-2004 07:07 AM
Re: print join to a file in Perl
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=
print "Enter OE HOST REMOTE:"; $oe_host_remote=
open (INP, "
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 $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 $_;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2004 07:23 AM
тАО07-27-2004 07:23 AM
Re: print join to a file in Perl
Is it truly as you show here?
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2004 07:30 AM
тАО07-27-2004 07:30 AM
Re: print join to a file in Perl
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.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2004 08:34 AM
тАО07-27-2004 08:34 AM
Re: print join to a file in Perl
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2004 08:39 AM
тАО07-27-2004 08:39 AM
Re: print join to a file in Perl
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2004 10:07 AM
тАО07-27-2004 10:07 AM
Re: print join to a file in Perl
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