How to write in multiple output files in perl?

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to write in multiple output files in perl?
# 1  
Old 10-12-2017
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.
# 2  
Old 10-12-2017
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

# 3  
Old 10-12-2017
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..
These 2 Users Gave Thanks to Corona688 For This Post:
# 4  
Old 10-13-2017
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
# 5  
Old 10-13-2017
Quote:
Originally Posted by Corona688
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 Smilie (the second is not though)..
# 6  
Old 10-14-2017
check out dup(). Find out more...
# 7  
Old 11-02-2017
dodona, that's exactly what dup() doesn't do.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Execute multiple files in multiple folders and also output on same folder

How to execute multiple files in multiple folders and also output to be generated in the same folder? Hello Team, I have a path like Sanity_test/*/* and it has around 100+ folders inside with files. I would like to run/execute those files and output of execution to be placed on same /... (1 Reply)
Discussion started by: pushpabuzz
1 Replies

2. Shell Programming and Scripting

Grep strings on multiple files and output to multiple files

Hi All, I want to use egrep on multiple files and the results should be output to multiple files. I am using the below code in my shell script(working in Ksh shell). However with this code I am not attaining the desired results. #!/bin/ksh ( a="/path/file1" b="path/file2" for file in... (4 Replies)
Discussion started by: am24
4 Replies

3. Shell Programming and Scripting

Create Multiple UNIX Files for Multiple SQL Rows output

Dear All, I am trying to write a Unix Script which fires a sql query. The output of the sql query gives multiple rows. Each row should be saved in a separate Unix File. The number of rows of sql output can be variable. I am able save all the rows in one file but in separate files. Any... (14 Replies)
Discussion started by: Rahul_Bhasin
14 Replies

4. Shell Programming and Scripting

Write two csv files into one excel with multiple sheets

I have requirement to write two CSV files to one single excel with multiple sheets. Data present in the two files should sit in excel as different sheets. How can we achieve this using shell script? 1.csv 2. csv 1,2,3,4 5,6,7,8 XXXXX YYYYY Res.excel 1.csv data... (1 Reply)
Discussion started by: duplicate
1 Replies

5. Shell Programming and Scripting

awk, multiple files input and multiple files output

Hi! I'm new in awk and I need some help. I have a folder with a lot of files and I need that awk do something in each file and print a new file with the output. The input file name should be modified when I print the outpu files. Thanks in advance for help! :-) ciao (5 Replies)
Discussion started by: gabrysfe
5 Replies

6. UNIX for Dummies Questions & Answers

Write the total number of rows in multiple files into another file

Hello Friends, I know you all are busy and inteligent too... I am stuck with one small issue if you can help me then it will be really great. My problem is I am having some files i.e. Input.txt1 Input.txt2 Input.txt3 Now my task is I need to check the total number of rows in... (4 Replies)
Discussion started by: malaya kumar
4 Replies

7. Shell Programming and Scripting

write to multiple files depending on file type (solved)

I want a script that will add comments to a file before check-in files. comments depend on type of files. i have a script to list files in the directory that will be checked-in There are xml, js, html, vm files. vm will use comments like c/c++ ( // or /*..*/) can you help me add a comment line... (0 Replies)
Discussion started by: pradeepmacha
0 Replies

8. UNIX for Dummies Questions & Answers

Using AWK: Extract data from multiple files and output to multiple new files

Hi, I'd like to process multiple files. For example: file1.txt file2.txt file3.txt Each file contains several lines of data. I want to extract a piece of data and output it to a new file. file1.txt ----> newfile1.txt file2.txt ----> newfile2.txt file3.txt ----> newfile3.txt Here is... (3 Replies)
Discussion started by: Liverpaul09
3 Replies

9. Shell Programming and Scripting

Help need to write a script on column separation for syslog output in perl

HI Pros, I have a issue.I need to write a script to parse the logs got from syslog server and update the same in my database.I need the following output.I donot know perl and I heard it very easy to write in perl I have the sample log I need each column seperated by commas and all equals... (0 Replies)
Discussion started by: iron_michael86
0 Replies

10. Shell Programming and Scripting

How to write bash script to explode multiple zip files

I have a directory full of zip files. How would I write a bash script to enumerate all the zip files, remove the ".zip" from the file name, create a directory by that name and unzip each zip file into its corresponding directory? Thanks! Siegfried (3 Replies)
Discussion started by: siegfried
3 Replies
Login or Register to Ask a Question