Capture output of open pipe to a file in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Capture output of open pipe to a file in perl
# 1  
Old 02-27-2014
Capture output of open pipe to a file in perl

Hi,

I am trying to capture the output of the an open pipe in perl. but I am not sure how to do this. can some one please help me do that?

Below is the script I am using

Quote:
#!/usr/bin/perl -w
use Digest::MD5 qw(md5 md5_hex md5_base64);
my $sep = chr(126);
my $md5gen ||="";
open ( my $DAT, "paflink filename |") or die "Could not open $ARGV[0]: $!";
while (my $line = <$DAT>) {
$line =~ s/\r?\n$//;
$line =~ s/^A //s;
my @fld = split $sep, $line;
my $md5txt="";
if ( ( !$fld[23] ) && ( $fld[19] && $fld[20] && $fld[21] ))
{
my $md5txt= $fld[19]." ".$fld[20]." ".$fld[21];
my $md5upper= uc($md5txt);
$md5gen=$md5upper;
}
elsif ( ( $fld[23] ) && ( $fld[19] && $fld[20] && $fld[21] ))
{
my $md5txt= $fld[23];
my $md5upper= uc($md5txt);
$md5gen=$md5upper;
}

if ( $md5gen ) {

print $line . $sep . md5_hex($md5gen) ."\n";

} else {

print $line . $sep ."\n";

}
undef $md5gen;
}
close $DAT;
# 2  
Old 02-27-2014
Replace this:

Code:
open ( my $DAT, "paflink filename |") or die "Could not open $ARGV[0]: $!";

With this:

Code:
open(my $DAT, "-|", "paflink filename") or die $!;

# 3  
Old 02-27-2014
Quote:
Originally Posted by Chubler_XL
Replace this:

Code:
open ( my $DAT, "paflink filename |") or die "Could not open $ARGV[0]: $!";

With this:

Code:
open(my $DAT, "-|", "paflink filename") or die $!;

Thanks - I tried the below code. It is able to capture the output in the Output.txt but its not capturing the output from the print lines which are within while loop.

Code:
open(my $DAT, "-|", " paflink -p /path/  ./InputFile.txt  Output.txt") or die $!;


Last edited by ahmedwaseem2000; 02-27-2014 at 11:24 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Capture error before pipe

Hi, I have a script that runs a tar command to standard out then pipes to a gzip: tar cfE - * | gzip -c > OUT.gz At the moment, even if the tar fails (e.g. because of lack of disk space), the gzip still runs successfully. Is there a way to make the whole line exit with a non-zero error... (6 Replies)
Discussion started by: Catullus
6 Replies

2. Shell Programming and Scripting

UNIX/PERL script to convert XML file to pipe delimited format

Hello, I need to get few values from a XML file and output needs to be written in another file with pipe delimited format. The Header & Footer of the Pipe Delimited file will be constant. The below is my sample XML file. I need to pull the values in between the XML tags <Operator_info to... (15 Replies)
Discussion started by: karthi1305561
15 Replies

3. UNIX for Dummies Questions & Answers

Use awk to pipe output from one file into multiple files

Hi All. Thanks for your help in advance. I have a requirement to examine the number of delimiters in each record of a file. If the record has the expected number of delimiters it should be passed into a 'good' file. If it does not, the record should be passed into a 'bad' file. I have been able... (8 Replies)
Discussion started by: codestar1
8 Replies

4. Shell Programming and Scripting

Want ro capture the debug in output file

I want to capture the debug for the below command in output file . i tried like this but its not working: sh -xv <scriptname> >> output.log i want the output in a log file. Anyone plz help in this (2 Replies)
Discussion started by: chakkaravarthy
2 Replies

5. Shell Programming and Scripting

How to capture output to log file

Hi I have a script that will run multiple unix & sql commands. I want to see the output as well as capture it to a log file for further analysis. Is there an easy way to do that instead of adding "tee -a logfile" on everyline or even on the execute line (i.e. script | tee -s logfile). Thanks (1 Reply)
Discussion started by: nimo
1 Replies

6. Shell Programming and Scripting

Capture Shell Script Output To A File

Hi, I am running a shell script called dbProcess.sh which performs shutdown and startup of various Oracle instances we have.At the time of execution the script produces the following output to the command line window $./dbProcess.sh stop #### Run Details ###### Hostname : server-hop-1... (4 Replies)
Discussion started by: rajan_san
4 Replies

7. Shell Programming and Scripting

capture output of file and send last string thereof to new file

Hello, If I run a program from within shell, the output is displayed in the command line terminal. Is there a way I can capture that output and choose only the very last string in it to send it to a new file? Thank you (6 Replies)
Discussion started by: Lorna
6 Replies

8. UNIX for Advanced & Expert Users

Capture output to file and printer

Hi All : I wanted a unix command by which I could be able to print the output to a file and at the same time to a printer. Any help will be greatly appreciated. Regards, Ramamurthy Dasari (1 Reply)
Discussion started by: rdasari
1 Replies

9. Shell Programming and Scripting

Failed to open output file Error

Hi guys, I Have written a script,In that it will call another file which contains the sql quaries. while wxecuting that I am getting the below exception 01/16|06:28:06:16800: Operating System Error|Failed to open output file Can anybody help me about this,,Its urgent (0 Replies)
Discussion started by: Anji
0 Replies

10. Shell Programming and Scripting

Capture output of program to file with limited filesize

I'm working on a script that will perform a backup, save a log of said backup and send the output to me in an email. Everything is working fine so far except that I can't figure out how to specify a maximum file size for the log file. I don't want a runaway log file jamming up the server.... (7 Replies)
Discussion started by: spectre_240sx
7 Replies
Login or Register to Ask a Question