Unix/Linux Go Back    


UNIX for Beginners Questions & Answers If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

How to write in multiple output files in perl?

UNIX for Beginners Questions & Answers


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-12-2017   -   Original Discussion by mad man
mad man mad man is offline
Registered User
 
Join Date: Nov 2015
Last Activity: 31 October 2017, 8:11 AM EDT
Posts: 49
Thanks: 25
Thanked 1 Time in 1 Post
How to write in multiple output files in perl?

hi,

Hope you are doing good.

During my coding yesterday i got this challenge, actually not a challenge it like to optimize the code.

I am printing some statement to monitor the file progress in the log file an also to display it in the screen. so i ended up in the below statements.


Code:
print "\n   --- The file  was uploaded successfully\n";
print LOGFILE "\n   --- The file was uploaded successfully\n";

The first print statement was showing the user in the screen about the progress and the second statement was printing the statement in the logfile.

So the challenge to me is how to make it in the single statement instead two statements. is it possible?

Thanks.
Sponsored Links
    #2  
Old Unix and Linux 10-12-2017   -   Original Discussion by mad man
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 18 November 2017, 12:20 AM EST
Location: Amsterdam
Posts: 11,617
Thanks: 516
Thanked 3,380 Times in 2,979 Posts
Hi, try:

Code:
print "\n   --- The file was uploaded successfully\n" | tee LOGFILE

or to append to the log file:

Code:
print "\n   --- The file was uploaded successfully\n" | tee -a LOGFILE

Sponsored Links
    #3  
Old Unix and Linux 10-12-2017   -   Original Discussion by mad man
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 17 November 2017, 9:25 PM EST
Location: Saskatchewan
Posts: 22,512
Thanks: 1,151
Thanked 4,273 Times in 3,946 Posts
Scrutinizer, this is perl, not shell.

You can use a function to make it more elegant:


Code:
sub log {
        my $arg=shift;
        print $arg;
        print LOGFILE $arg;
}

log("hey guys");


Last edited by Corona688; 10-12-2017 at 03:48 PM..
The Following 2 Users Say Thank You to Corona688 For This Useful Post:
MadeInGermany (10-13-2017), Scrutinizer (10-13-2017)
    #4  
Old Unix and Linux 10-13-2017   -   Original Discussion by mad man
apmcd47 apmcd47 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 17 November 2017, 11:04 AM EST
Posts: 266
Thanks: 11
Thanked 72 Times in 69 Posts
My perl is a little rusty, but how about:

Code:
open(LOG,"| tee logfile") or die "cannot open logfile";
...
print LOG "message\n";

However Corona's function allows for greater flexibility, ie different formatting for terminal and log file, or sending output to multiple log files.

Andrew
Sponsored Links
    #5  
Old Unix and Linux 10-13-2017   -   Original Discussion by mad man
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 18 November 2017, 12:20 AM EST
Location: Amsterdam
Posts: 11,617
Thanks: 516
Thanked 3,380 Times in 2,979 Posts
Quote:
Originally Posted by Corona688 View Post
Scrutinizer, this is perl, not shell.
Oops, apparently did not read the last word in the title.

I thought it was Korn Shell, because he first is also a perfectly legitimate ksh command Linux (the second is not though)..
Sponsored Links
    #6  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by mad man
dodona dodona is offline
Registered User
 
Join Date: Jun 2012
Last Activity: 17 November 2017, 2:57 AM EST
Posts: 11
Thanks: 10
Thanked 1 Time in 1 Post
check out dup(). Find out more...
Sponsored Links
    #7  
Old Unix and Linux 2 Weeks Ago   -   Original Discussion by mad man
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 17 November 2017, 9:25 PM EST
Location: Saskatchewan
Posts: 22,512
Thanks: 1,151
Thanked 4,273 Times in 3,946 Posts
dodona, that's exactly what dup() doesn't do.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grep strings on multiple files and output to multiple files am24 Shell Programming and Scripting 4 04-20-2016 03:17 AM
Create Multiple UNIX Files for Multiple SQL Rows output Rahul_Bhasin Shell Programming and Scripting 14 01-09-2014 06:11 AM
Write two csv files into one excel with multiple sheets duplicate Shell Programming and Scripting 1 12-09-2013 02:45 AM
Using AWK: Extract data from multiple files and output to multiple new files Liverpaul09 UNIX for Dummies Questions & Answers 3 10-12-2010 04:59 AM
Help need to write a script on column separation for syslog output in perl iron_michael86 Shell Programming and Scripting 0 10-26-2009 11:58 AM



All times are GMT -4. The time now is 02:38 AM.