简体   繁体   English

bash 一个文件夹中的文件与另一个文件夹中的所有文件循环

[英]bash loop file in one folder against all files in another folder

I am trying to do a ( bedtools ) intersect.我正在尝试做一个( bedtools )相交。 Anyone see what is wrong with my bash code here?有人在这里看到我的 bash 代码有什么问题吗?

#!/bin/bash

dir=$(pwd)
query=$dir/query_beds
pgs=$dir/pgs_beds

for file in $query/*; do 
   bedtools intersect -wa -wb -a $file -b $pgs/* -c -C -sorted -filenames
done > ${file%.*}-results.txt

"query" directory contains many files which need to each be iteratively queried against many files in "pgs" directory using the package bedtools and command intersect, which allows an asterisk for file "-b" to cycle multiple files. “query”目录包含许多文件,每个文件都需要使用 package bedtools 和命令 intersect 对“pgs”目录中的许多文件进行迭代查询,这允许文件“-b”的星号循环多个文件。

Per usage:每次使用:

One or more BAM/BED/GFF/VCF file(s) “B”. Use “stdin” if passing B with a UNIX pipe.
NEW!!!: -b may be followed with multiple databases and/or wildcard (*) character(s).

I believe the problem is with $pgs/*, looping one file against multiple files in another directory and I may need to restructure my loop to change directory or something.我认为问题出在 $pgs/*,将一个文件循环到另一个目录中的多个文件,我可能需要重组我的循环以更改目录或其他内容。

UPDATE:更新:

I have updated the script to work and was able to get everything working with:我已经更新了脚本来工作并且能够让所有的东西都工作:

#!/bin/bash -x
dir=$(pwd)
query=$dir/query_beds
pgs=$dir/pgs_beds
for f in $(ls $query/*.bed); do 
   for g in $(ls $pgs/*); do
      bedtools intersect -wa -wb -a $f -b $g* -C -filenames
   done > $(basename $f .ext).txt
done

In your code the output of the whole loop is redirected to one file.在您的代码中,整个循环的 output 被重定向到一个文件。 Maybe you want something like below (redirect the output of each bedtool to a separate file).也许你想要像下面这样的东西(将每个 bedtool 的 output 重定向到一个单独的文件)。

Also note that if you use ${file%.*} the outputs will be created in the $query directory so executing the script twice might result in unexpected results.另请注意,如果您使用${file%.*} ,输出将创建在$query目录中,因此执行脚本两次可能会导致意外结果。 I would recommend to use $(basename $file.ext).txt (where.ext is the extension of the files in $query . The basename command keeps only the name of the file (so without the directory) and also removes the.ext if present.我建议使用$(basename $file.ext).txt (where.ext 是$query中文件的扩展名。basename 命令只保留文件的名称(因此没有目录)并且还删除了。分机如果存在。

#!/bin/bash

dir=$(pwd)
query=$dir/query_beds
pgs=$dir/pgs_beds

for file in $query/*; do 
   bedtools intersect -wa -wb -a $file -b $pgs/* -c -C -sorted -filenames > ${file%.*}-results.txt
done 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有特定模式的文件夹中文件的 Bash 循环 - Bash loop on files in folder without specific pattern Bash脚本对文件夹中的所有文件执行python包 - Bash script to execute a python package on all files in a folder 使文件夹中的所有文件与最长的文件一样长 - Make all files in a folder as long as the longest file 如何遍历文件夹中的文件以创建参数列表以传递到Windows批处理文件中的另一个脚本中? - How do I loop through files in folder to create a parameter list to pass into another script in a Windows batch file? 如何在批处理文件中使用for循环列出文件夹中所有文件和目录的名称 - How to list the names of all the files and directories in a folder using for loop in a batch file 重命名文件夹中的所有文件 - Renaming all files in the folder 使用批处理将某个文件夹中文本文件中包含的所有数据编译为一个文件 - compile all data contained in text files in a certain folder into one file with batch scipt 如何使用命令COPY将一个文件夹中的所有MP3文件合并为一个MP3文件? - How to use command COPY to merge all MP3 files in a folder together to one MP3 file? 循环用于使用Dejavu库识别文件夹中的所有文件以进行音频指纹识别 - Loop for recognizing all files in a folder using Dejavu library for audio fingerprinting VBA遍历文件夹中的所有文件并删除无效字符 - VBA Loop through all files in folder and remove invalid characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM