write in file and STDOUT from perl script [duplicate] - perl

This question already has answers here:
How to get perl code output to STDOUT/STDERR and a file, in realtime and cross-platform?
(5 answers)
Closed 9 years ago.
Hi I need to have perl script's output in both file and STDOUT but I cannot redirect output from shel like ./a.pl > out.log. Is it possible?

Obviously, you should be using
./a.pl | tee out.log
but it sounds like you will reject that. Next best is probably File::Tee.
use File::Tee qw( tee );
tee(STDOUT, '>', 'out.log');

Try this instead:
a.pl | tee out.log

Related

Modify print job with one liner - missing output command [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am using Perl with our printing output system. Usually we call perl scripts, but I am just replacing a keyword with another word. In a console and a test file this worked with this one liner:
perl -pi -e "s/KEYWORD1/KEYWORD2"
Now I wanted to use it with our system and it gives me the error that there is no output file created. As I am fairly new to the field of Perl and want to have it used within a one liner, how can i do this?
Thank you very much for your help.
Kind regards
As Shawn mentioned in the comment, you are missing / in the substitution command s///. You are also missing a filename, which is needed for the -i option.
Both of these fail and print a warning and an error:
perl -pi -e "s/KEYWORD1/KEYWORD2" < foo
# or:
cat foo | perl -pi -e "s/KEYWORD1/KEYWORD2"
They print messages:
-i used with no filenames on the command line, reading from STDIN.
Substitution replacement not terminated at -e line 1.
Correct usage:
perl -pi -e 's/KEYWORD1/KEYWORD2/' foo
# or:
perl -pe 's/KEYWORD1/KEYWORD2/' < foo
# or:
another_command | perl -pe 's/KEYWORD1/KEYWORD2/'

How can I convert my shell script to Perl? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a shell script:
#!usr/bin/bash
branch_name=$1
task_name=$2
createviewset ccm -b $branch_name -t $task_name
source setenv $task_name
rest of the code
Now I want to convert this script to a Perl script. How can I do that? So far what I have written in the code but this code does not seem to work.
!/usr/bin/perl
use warnings;
use strict;
my branch_name;
my task_name;
createviewset ccm -b $branch_name -t $task_name
source setenv $task_name
Here the createviewset is an existing script which I am calling here.
You should visit http://perlmaven.com/ (available in many languages) or http://learn.perl.org/ to learn some Perl first.
Your shell script doesn't need to copy the commandline values. You also used #!usr/bin/bash which won't work because the path is either /usr/bin/bash or (more common) /bin/bash:
#!/bin/bash
createviewset ccm -b $1 -t $2
source setenv $2
rest of the code
Perl assigns all command line arguments to the array #ARGV. This sample prints your two arguments:
#!/usr/bin/perl
print $ARGV[0];
print $ARGV[1];
Notice that the numbering starts with 0 instead of 1 as in $1 in your bash script.
Next part is running external (shell) commands in Perl: Use the system command.
#!/usr/bin/perl
use strict;
use warnings;
system 'createviewset','ccm','-b',$ARGV[0],'-t',$ARGV[1];
system 'source','setenv',$ARGV[1];
Notice that the source command won't work because a Perl script is not a shell script and can't "include" Bash script. I appreciate that you're trying to use Perl for your problem, but it looks like Bash is the much better tool for this.

Redirecting stdout and stderr output to a file using system command doesn't work in perl [duplicate]

This question already has answers here:
How to redirect and append both standard output and standard error to a file with Bash
(8 answers)
Closed 8 years ago.
When I try to redirect stdout and stderr output of a command onto a file. There is nothing which gets written into a file. This is in a Perl script. I want the stdout and stderr output of a command to be redirected to a file. The code I wrote is this:
system("sudo rm a.txt &>> output.txt");
The file output.txt doesn't contain anything at all. Can someone help me with this?
system("sudo rm a.txt >> output.txt 2>&1")

perl line-mode oneliner with ARGV [duplicate]

This question already has answers here:
How can I process options using Perl in -n or -p mode?
(2 answers)
Closed last year.
I often need to run some Perl one-liners for fast data manipulations, like
some_command | perl -lne 'print if /abc/'
Reading from a pipe, I don't need a loop around the command arg filenames. How can I achieve the next?
some_command | perl -lne 'print if /$ARGV[0]/' abc
This gives the error:
Can't open abc: No such file or directory.
I understand that the '-n' does the
while(<>) {.... }
around my program, and the <> takes args as filenames, but doing the next every time is a bit impractical
#/bin/sh
while read line
do
some_command | perl -lne 'BEGIN{$val=shift #ARGV} print if /$val/' "$line"
done
Is there some better way to get "inside" the Perl ONE-LINER command line arguments without getting them interpreted as filenames?
Some solutions:
perl -e'while (<STDIN>) { print if /$ARGV[0]/ }' pat
perl -e'$p = shift; while (<>) { print if /$p/ }' pat
perl -e'$p = shift; print grep /$p/, <>' pat
perl -ne'BEGIN { $p = shift } print if /$p/' pat
perl -sne'print if /$p/' -- -p=pat
PAT=pat perl -ne'print if /$ENV{PAT}/'
Of course, it might make more sense to create a pattern that's an ORing or all patterns rather than executing the same command for each pattern.
Also reasonably short:
... | expr=abc perl -lne 'print if /$ENV{expr}/'
Works in bash shell but maybe not other shells.
It depends on what you think will be in the lines you read, but you could play with:
#/bin/sh
while read line
do
some_command | perl -lne "print if /$line/"
done
Clearly, if $line might contain slashes, this is not going to fly. Then, AFAIK, you're stuck with the BEGIN block formulation.

Running the /usr/bin/time from perl [duplicate]

This question already has an answer here:
How do I use backticks to capture the elapsed time output from time(1)?
(1 answer)
Closed 9 years ago.
I'm trying to do some very-rough benchmarking and so I'd like to run the time command from my script. I have the following:
#!/usr/bin/perl
use strict;
my $command = "/usr/bin/time -f \"%U,%S,%E,%P,%K,%M\" ...";
my $stats = `$command`;
print "stats: $stats\n";
Unfortunately, it looks like the result of the command is never assigned to $stats. When I execute the script, I get something like the following:
0.15,0.03,0:00.44,43%,0,143808
stats:
So it looks like it runs the time command successfully, but prints out the value to STDOUT instead of assigning the value to $stats. When I use another command, like ls, it seems to work as expected. What am I doing wrong here?
time prints to stderr.
$ /usr/bin/time -f "%U,%S,%E,%P,%K,%M" echo foo >/dev/null
0.00,0.00,0:00.03,10%,0,2352
$ /usr/bin/time -f "%U,%S,%E,%P,%K,%M" echo foo >/dev/null 2>/dev/null
$
So just add 2>&1 to your command.
time writes to standard error, so you need to redirect it to standard output to capture it with Perl's backticks
my $command = "/usr/bin/time -f \"%U,%S,%E,%P,%K,%M\" ... 2>&1";