Reputation: 4436
Initially I have run the shell script and direct the output into a file and then use Perl to reading the file and parse it. I wanted to put these two steps together into one Perl script.
And I tried to write something like this:
my $in = 'file.txt';
my $read = qx(samtools view -S $in |cut -f 3|sort|uniq -c|awk '{OFS="\t";print \$2,\$1}');
open (IN,"<",$read) || die "cannot open $read: $!";
I got the error message that $in can't be opened and the result from the qx() command was printed out on the screen.
I couldn't understand why this didn't work and how to make it work..
EDIT:
The error from the open line is:
File name too long
Upvotes: 0
Views: 1363
Reputation: 808
Your code will read the output of the shell commands that you have run and store that output (anything written to STDOUT) in $in
.
I doubt the shell command will run correctly as you are attempting to use $in
within it, and it is probably not defined.
You can spot this if you use strict;
at the top of the script.
my $in = qx(samtools view -S $in |cut -f 3|sort|uniq -c|awk '{OFS="\t";print \$2,\$1}');
// No need to open anything here, just inspect $in
I'm not familiar with 'samtools', but you will likely need to fix the shell command and use the right thing instead of $in
in view -S $in
EDIT:
Okay, you're saying that your shell command writes to a file, which you then want to read. I misunderstood!
What I have stated above remains true, but to solve your issue more fully you will need to do something like this:
my $in = 'file.txt';
my $cmd_output = qx(samtools view -S $in |cut -f 3|sort|uniq -c|awk '{OFS="\t";print \$2,\$1}');
open my $fh, '<', $in or die "ERROR: Couldn't open $in for reading: $!";
2ND EDIT, answering question in comments:
In order to parse the output line by line, you can just do something like this:
foreach my $line (split /\n/, $cmd_output) {
// Do something with $line
}
To be honest, you might even find it easier to do this on the shell with:
$ samtools view -S $in |cut -f 3|sort|uniq -c|awk '{OFS="\t";print \$2,\$1}' | perl -lne '... do something with $_ ...'
Upvotes: 3
Reputation: 57590
The string produced by the shell command probably contains a newline at the end. Remove it via chomp
:
chomp $in;
Also, please use normal variables for file handles:
open my $fh, "<", $in or die qq(Can't open "$in": $!);
Upvotes: 4