linghu2049的个人博客分享 http://blog.sciencenet.cn/u/linghu2049

博文

perl基础:调用linux系统命令并批量处理文件问题(原创)

已有 6826 次阅读 2011-10-17 14:35 |个人分类:perl|系统分类:科研笔记|关键词:学者| 系统命令

我在linux系统下面运行命令: macs14 -t CD4-H3K9ac.bed -f BED -g hs --bw 200 -n test
(
这里面macs14是一个软件CD4-H3K9ac.bed为处理得文件名,其他全部是相应参数)
但是,每运行一次,只能执行一个文件。我手上现在有CD4-H3K9ac.bed CD4-H3K8ac.bed CD4-H3K7ac.bed .....
等等类似的文件,都放在同一目录下,我想些一个perl脚本,将这些文件都一次加到linux命令中,这样我只要运行perl一次,就能把所有文件都处理完。

 

解决办法如下~~
step1:
读取文件列表并存入text文件

#!/usr/bin/perl

use warnings;
use strict;
my $dir ='D:\data\tem2';
my $file;
opendir (DIR, $dir);
my @dir = readdir DIR;
open(SAVEOUT,">&STDOUT");
open(STDOUT,"+>test.txt");

 foreach $file (@dir) {
    if ( $file =~ /[A-Z]*\.bed/) {
 print $file,"t";
 }
}

open(STDOUT,">&SAVEOUT"); #记得最后恢复STDOUT
closedir(DIR);
close STDOUT;
close SAVEOUT;

 

step2:调用system命令

 

#!/usr/bin/perl  

use strict;

use warnings;

my $file;

my @files= qw /CD4-H3K8ac.bed CD4-H3K9ac.bed CD4-H3K10ac.bed  /;  #这个地方若改为qw /test.txt /不行

foreach $file (@files){

print $file;

system ("macs14 -t $file -f BED -g hs --bw 200 --llocal 5000 -n test-$file");

}

 

然后就解决了。

但是就qw那句若改为qw /test.txt /这个命令时,得不到想要的文件名,只能得到test.txt ,不知何故,所以我干脆将文件列在qw/ /之间得了~~
现在还没有解决,但是问题太初级,所以继续赶小骆驼吧~~,以后可能会明白的。



https://m.sciencenet.cn/blog-634847-497796.html

上一篇:bioinformatics: Most-Cited Articles as of October 1, 2011
下一篇:perl基础:perl中@_,$_和$1,$2,...及其类似变量的含义

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-6-3 16:11

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部