perl中的unix函数

perl中的unix函数,perl,bash,shell,unix,Perl,Bash,Shell,Unix,我试图在perl驱动程序脚本中使用一些unix工具,因为我对编写shell脚本知之甚少。我的目的是将几个简单的unix命令组合在一起,以便在一个perl命令中在100个目录上运行脚本 任务是我有100多个文件夹,每个文件夹中有n个文件。我想在每个文件夹上做同样的事情,那就是合并其中的文件,对合并后的文件进行排序,并使用bedtools合并重叠区域(生物信息学中非常常见的做法) 以下是我所拥有的: #!/usr/bin/perl -w use strict; my $usage =" This

我试图在perl驱动程序脚本中使用一些unix工具,因为我对编写shell脚本知之甚少。我的目的是将几个简单的unix命令组合在一起,以便在一个perl命令中在100个目录上运行脚本


任务是我有100多个文件夹,每个文件夹中有n个文件。我想在每个文件夹上做同样的事情,那就是合并其中的文件,对合并后的文件进行排序,并使用bedtools合并重叠区域(生物信息学中非常常见的做法)

以下是我所拥有的:

#!/usr/bin/perl -w
use strict;

my $usage ="
This is a driver script to merge files in each folder into one combined file
";
die $usage unless @ARGV;

my ($in)=@ARGV;
open (IN,$in)|| die "cannot open $in";

my %hash;
my $final;

while(<IN>){
    chomp;
    my $tf = $_;
    my @array =`ls $tf'/.'`;
    my $tmp;
    my $tmp2;
    foreach my $i (@array){
        $tmp = `cut -f 1-3 $tf'/'$i`;
        $tmp2 = `cat $tmp`;
    }
    my $tmp3;
    $tmp3=`sort -k1,1 -k2,2n $tmp2`;
    $final = `bedtools merge -i $tmp3`;
}
print $final,"\n";
问题是如何将输出定向到perl中的另一个变量,然后在另一个unix命令中使用该变量


请让我知道,如果你能指出我可以改变,使其工作。非常感谢

要在shell中使用perl变量,下面是一个示例:

#!/usr/bin/env perl

my $var = "/etc/passwd";

my $out = qx(file $var);

print "$out\n";

对于其他人来说,这是非常混乱的。您应该花点时间学习perl,不要将coreutils命令和perl混用,因为perl本身是一个更好的工具,可以用来开整个玩笑。

backticks的输出通常包括换行符,通常在使用下游输出之前必须删除换行符。在代码中添加一些
chomp

chomp( my @array =`ls $tf'/.'` );

my $tmp;
my $tmp2;
foreach my $i (@array){
    chomp( $tmp = `cut -f 1-3 $tf'/'$i` );
    chomp( $tmp2 = `cat $tmp` );
}
my $tmp3;
chomp( $tmp3=`sort -k1,1 -k2,2n $tmp2` );
$final = `bedtools merge -i $tmp3`;

嗯。我放弃了perl,决定尝试使用shell脚本。成功了!! 谢谢你的回答

for dir in `ls -d */`
do
    name=$(basename $dir /)
    cd $dir
    for file in `ls`
    do
        cut -f 1-3 $file > $file.tmp
    done
    for x in `ls *tmp`
    do
        cat $x >> $name.tmp1
    done
    sort -k1,1 -k2,2n $name.tmp1 > $name.tmp2
    bedtools merge -i $name.tmp2 > $name.combined
done

您可能知道shell脚本,但不知道它是好的实践:永远不要解析
ls
的输出是您应该首先学习的内容之一!亲爱的,您将一种漂亮的语言(Perl)与最糟糕的shell脚本实践相结合!(无意冒犯)。你能解释一下你的脚本应该做什么吗?我有100多个文件夹,每个文件夹中有n个文件。我想在每个文件夹上做同样的事情,那就是合并其中的文件,对合并后的文件进行排序,并使用bedtools合并重叠区域(生物信息学中非常常见的做法)
for dir in `ls -d */`
do
    name=$(basename $dir /)
    cd $dir
    for file in `ls`
    do
        cut -f 1-3 $file > $file.tmp
    done
    for x in `ls *tmp`
    do
        cat $x >> $name.tmp1
    done
    sort -k1,1 -k2,2n $name.tmp1 > $name.tmp2
    bedtools merge -i $name.tmp2 > $name.combined
done