使用bash命令进行文件转换

时间:2017-08-10 08:47:11

标签: bash

我在同一目录中有许多文件,并希望使用bash命令(# Learn more about services, parameters and containers at # https://symfony.com/doc/current/service_container.html parameters: #parameter_name: value services: # default configuration for services in *this* file _defaults: # automatically injects dependencies in your services autowire: true # automatically registers your services as commands, event subscribers, etc. autoconfigure: true # this means you cannot fetch services directly from the container via $container->get() # if you need to do this, you can override this setting on individual services public: false # makes classes in src/AppBundle available to be used as services # this creates a service per class whose id is the fully-qualified class name AppBundle\: resource: '../../src/AppBundle/*' # you can exclude directories or files # but if a service is unused, it's removed anyway exclude: '../../src/AppBundle/{Entity,Repository,Tests}' # controllers are imported separately to make sure they're public # and have a tag that allows actions to type-hint services AppBundle\Controller\: resource: '../../src/AppBundle/Controller' public: true tags: - { name: "controller.service_arguments" } # add more services, or override services that need manual wiring # AppBundle\Service\ExampleService: # arguments: # $someArgument: 'some_value' admin.category: class: AppBundle\Admin\CategoryAdmin arguments: [~, AppBundle\Entity\Category, ~] tags: - { name: sonata.admin, manager_type: orm, label: Category } public: true admin.blog_post: class: AppBundle\Admin\BlogPostAdmin arguments: [~, AppBundle\Entity\BlogPost, ~] tags: - { name: sonata.admin, manager_type: orm, label: Blog post } public: true AppBundle\Service\FileUploader: arguments: targetDir: '%brochures_directory%' .sam)将它们转换为另一个文件。我用bash写了这个小命令。它工作正常,但问题是所有生成的文件将具有相同的名称,因此相互替换,最后我只有一个文件。你知道如何改变这个命令,以便我分别获得每个.bam文件的.bam文件吗?

.sam

3 个答案:

答案 0 :(得分:0)

我没有与bash合作,但多年来unix管理员给了我一个例子来改变文件中的换行符(Unix到Win,反之亦然):

 for i in `ls *.conf`
 do
   unix2dos <$i >$i.new
 done

所以这是假设的方式:

 for i in `ls ${SAM_INDIR}/*.sam`
 do
    samtools view -Sb <$i >${BAM_OUTDIR}/$i.bam
 done

答案 1 :(得分:0)

你也可以试试这个:

BAM_OUTDIR="bam_files"
SAM_INDIR="sam_files"

ls -1 ${SAM_INDIR}/*.sam | while read file; do

  echo "conversion of sam to bam started ..................."
  dn=$(dirname "$file");
  new_bn=$(basename "$file" | sed s%sam$%bam%);
  output="${BAM_OUTDIR}/$new_bn";
  samtools view -Sb "${file}" > "${output}"
done

答案 2 :(得分:0)

BAM_OUTDIR="bam_files"
SAM_INDIR="sam_files"
for file in $SAM_INDIR/*.sam; do 
    echo "conversion of sam to bam started ..................."
    base=$(basename $file); 
    samtools view -Sb "${file}" > "${BAM_OUTDIR}/${base%.sam}.bam"
done

而不是解析输出ls - don't parse the output of ls - 这使用shell globs迭代$ SAM_INDIR中的所有* .sam文件。然后它使用中间变量base来保存传入的文件名,而不使用$ SAM_INDIR部分。然后samtools命令将输出重定向到$ BAM_OUTDIR中的文件,通过删除&#34; .sam&#34;从基本文件名的末尾开始,然后附加&#34; .bam&#34;。