击:替换文件名的一部分 [英] Bash: replace part of filename

查看:161
本文介绍了击:替换文件名的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个命令我想对所有文件夹的文件中运行,该命令的语法如下:

 顶礼帽-o<&OUTPUT_FILE GT; <&INPUT_FILE GT;

我希望做的是,循环通过在任意文件夹中的所有文件,并使用输入文件名来创建类似,但不同的输出文件名的脚本。文件名看起来像这样:

 输入名称所需的输出名称
路径/要/ sample1.fastq路径/要/ sample1.bam
路径/要/ sample2.fastq路径/要/ sample2.bam

获取输入工作看起来很简单:

 在* .fastq˚F

     高顶礼帽-o<&OUTPUT_FILE GT; $ F
DONE

我试着用输出= $ {楼.fastq,.bam} ,并将它作为输出参数,但是,这并不工作。我得到的是一个错误: 3号线:$ {楼.fastq,.bam}:替代不好。这是做我想做的方式,或者我应该做点别的?如果这是正确的做法,我是什么做错了吗?

感谢所有的答案!奖金的问题,虽然...如果我有一个名为像这样的文件,而不是:

 路径/到/ sample1_1.fastq
路径/到/ sample1_2.fastq
路径/到/ sample2_1.fastq
路径/到/ sample2_2.fastq
...

...在那里我可以有样品( sampleX )的任意数,但个个都与他们有关(两个文件 _1 _2 )。该命令现在看起来是这样的:

 顶礼帽-o<&OUTPUT_FILE GT; <&INPUT_1 GT; <&INPUT_2 GT;

所以,还是有只是一个输出,这是我可以做类似$ {F / _ [1-2] .fastq / .bam} ,但我不能确定如何获得循环迭代只超过一次每 sampleX 在同一时间服用这两个相关的文件...想法?

所以,这是没有的伎俩最终的脚本!

 在* _1.fastq˚F

        高顶礼帽-o$ {F / _1.fastq / .bam}$ F$ {F / _1.fastq / _2.fasq}
DONE


解决方案

您可以使用:

 顶礼帽-o$ {F / .fastq / .bam}$ F

测试:

  F ='路径/要/ sample1.fastq
回声$ {F / .fastq / .bam}
路径/到/ sample1.bam

I have a command I want to run on all of the files of a folder, and the command's syntax looks like this:

tophat -o <output_file> <input_file>

What I would like to do is a script that loops over all the files in an arbitrary folder and also uses the input file names to create similar, but different, output file names. The file names looks like this:

input name                desired output name
path/to/sample1.fastq     path/to/sample1.bam
path/to/sample2.fastq     path/to/sample2.bam

Getting the input to work seems simple enough:

for f in *.fastq
do
     tophat -o <output_file> $f
done

I tried using output=${f,.fastq,.bam} and using that as the output parameter, but that doesn't work. All I get is an error: line 3: ${f,.fastq,.bam}: bad substitution. Is this the way to do what I want, or should I do something else? If it's the correct way, what am I doing wrong?

[EDIT]:

Thanks for all the answers! A bonus question, though... What if I have files named like this, instead:

path/to/sample1_1.fastq
path/to/sample1_2.fastq
path/to/sample2_1.fastq
path/to/sample2_2.fastq
...

... where I can have an arbitrary number of samples (sampleX), but all of them have two files associated with them (_1 and _2). The command now looks like this:

tophat -o <output_file> <input_1> <input_2>

So, there's still just the one output, for which I could do something like "${f/_[1-2].fastq/.bam}", but I'm unsure how to get a loop that only iterates once over every sampleX at the same time as taking both the associated files... Ideas?

[EDIT #2]:

So, this is the final script that did the trick!

for f in *_1.fastq
do
        tophat -o "${f/_1.fastq/.bam}"  $f "${f/_1.fastq/_2.fasq}"
done

解决方案

You can use:

tophat -o "${f/.fastq/.bam}" "$f"

Testing:

f='path/to/sample1.fastq'
echo "${f/.fastq/.bam}"
path/to/sample1.bam

这篇关于击:替换文件名的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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