阅读UNIX和运行命令的文件列表 [英] Read list of files on unix and run command

查看:169
本文介绍了阅读UNIX和运行命令的文件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pretty在shell脚本新的,我一直在挣扎了一整天弄清楚如何执行为命令。从本质上讲,我试图做的是以下内容:

I am pretty new at shell scripting and I have been struggling all day to figure out how to perform a "for" command. Essentially, what I am trying to do is the following:

我有一堆名字的​​LIST.TXT文件:

I have a list.txt file with a bunch of names:

name1
name2
name3

列表中的每一个名字,有两个不同的文件,每一个不同的结局的名称。例如:

for every name in the list, there are two different files, each with a different ending to the name. Ex:

name1_R1
name1_R2

我想运行的程序叫做镰刀。基本上,它需要两个文件(对应于彼此)和运行在他们的分析,因此需要我有这个命名方案。镰命令如下:

The program I am trying to run is called sickle. Basically, it takes two files (that correspond to each other) and runs an analysis on them, hence requiring me to have this naming scheme. The sickle command is as follow:

sickle pe -f input_file1.fastq -r input_file2.fastq -t sanger \

如果有人可以帮助我,至少只是告诉我如何让UNIX读取文件的列表,并把每一行独立,我想我可以从那里走。我尝试了一些东西,但没有一次成功。

If someone could help me out, at least just by telling me how to get unix to read the list of files and treat each line independently I think I could go from there. I tried a few things, but none of them worked.

推荐答案

有几个方法可以做到这一点。由于名字中的数据文件'每行一个',我们可以假设有在文件名中没有换行。

There are a couple of ways to do it. Since the names are 'one per line' in the data file, we can assume there are no newlines in the file names.

for file in $(<list.txt)
do
    sickle pe -f "${file}_file1.fastq" -r "${file}_file2.fastq" -t sanger
done

,而循环与

while read file
do
    sickle pe -f "${file}_file1.fastq" -r "${file}_file2.fastq" -t sanger
done < list.txt

如果有在名称(也没有其他的空格字符,如制表符)没有空格的循环才有效。在,而循环是干净的,只要你没有在名称换行,虽然使用同时读取-r文件会给你应对不可预知的甚至更好的保护。周围的文件名中的双引号中的循环是装饰性的(但无害的),因为文件名不能包含空格,但那些在,而< /含code>循环prevent文件名从被拆分的时候,他们不应该被拆分空白。这是经常引用变量每次使用它们的时间是个好主意,但它当变量可能包含空格,但你不想要的值分手严格只是重要的。

The for loop only works if there are no blanks in the names (nor other white-space characters such as tabs). The while loop is clean as long as you don't have newlines in the names, though using while read -r file would give you even better protection against the unexpected. The double quotes around the file name in the for loop are decorative (but harmless) because the file names cannot contain blanks, but those in the while loop prevent file names containing blanks from being split when they should not be split. It's often a good idea to quote variables every time you use them, though it strictly only matters when the variable might contain blanks but you don't want the value split up.

我猜应该传递给镰刀什么名字,因为你的问题命令不清楚了 - 我敢肯定,99%我已经猜到错了,但它在不同的后缀匹配您的示例命令假设文件的基本名称是输入。我省略尾部的反斜杠;它是逃离的性格和目前尚不清楚你真正想要在那里。

I've had to guess what names should be passed to the sickle command since your question is not clear about it — I'm 99% sure I've guessed wrong, but it matches the different suffixes in your sample command assuming the base name of file is input. I've omitted the trailing backslash; it is the 'escape' character and it is not clear what you really want there.

这篇关于阅读UNIX和运行命令的文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆