Apply command line for some files.

huangapple go评论67阅读模式
英文:

Apply command line for some files

问题

我是一名在bash中的初学者,遇到了一个将命令应用于文件夹中所有文件的问题。以下是相关命令的内容:

for samples in ~/work/RADseq/Pool1_process_radtags/*
do
  /usr/local/bioinfo/src/BBMap/bbmap_38.95/reformat.sh in=$samples.1.fq in2=$samples.2.fq out=$samples-reform out2=$samples-reform2 addslash=true
done

in= 命令应该考虑以 .1.fq 结尾的所有文件,而 in2= 命令应该考虑以 .2.fq 结尾的所有文件。例如:

/usr/local/bioinfo/src/BBMap/bbmap_38.95/reformat.sh in=XY1.1.fq in2=XY1.2.fq out=XY1.1-reform.fq out2=XY1.2-reform.fq addslash=true

同样的规则适用于 XY2XY15XY18

英文:

I am a beginner in bash and have a problem applying a command to all the files in a folder. Here is the command in question:

for samples in ~/work/RADseq/Pool1_process_radtags/*
do
  /usr/local/bioinfo/src/BBMap/bbmap_38.95/reformat.sh in=$samples.1.fq in2=$samples.2.fq out=$samples-reform out2=$samples-reform2 addslash=true
done

This is what the contents of the file Pool1_process_radtags look like:

XY1.1.fq
XY1.2.fq
XY2.1.fq
XY2.2.fq
XY15.1.fq
XY15.2.fq
XY18.1.fq
XY18.2.fq

The command in= must take into account all files ending with .1.fq and in2= with .2.fq

Here is an example :

/usr/local/bioinfo/src/BBMap/bbmap_38.95/reformat.sh in=XY1.1.fq in2=XY1.2.fq out=XY1.1-reform.fq out2=XY1.2-reform.fq addslash=true

And the same for XY2, XY15 and XY18

答案1

得分: 2

Your command

for samples in ~/work/RADseq/Pool1_process_radtags/*

will loop over all files, both .1.fq and .2.fq.

I assume you want to run the command once for every pair of .1.fq and .2.fq files and that the files always exist in pairs. That's why I change the pattern to match the .1.fw files only and used ${variable%pattern} to remove this trailing part of the file name. For example, the input file XY1.1.fq will result in samples="~/work/RADseq/Pool1_process_radtags/XY1", and the script will append trailing parts as necessary.

I also added quoting to prevent possible problems in case of file names with spaces or special characters.

for fw1file in ~/work/RADseq/Pool1_process_radtags/*.1.fw
do
    samples="${fw1file%.1.fw}"
    /usr/local/bioinfo/src/BBMap/bbmap_38.95/reformat.sh in="$samples".1.fq in2="$samples".2.fq out="$samples".1-reform.fq out2="$samples".2-reform.fq addslash=true
done

(You can use https://www.shellcheck.net to find errors in shell scripts.)

英文:

Your command

for samples in ~/work/RADseq/Pool1_process_radtags/*

will loop over all files, both .1.fq and .2.fq.

I assume you want to run the command once for every pair of .1.fq and .2.fq files and that the files always exist in pairs. That's why I change the pattern to match the .1.fw files only and used ${variable%pattern} to remove this trailing part of the file name. For example, the input file XY1.1.fq will result in samples="~/work/RADseq/Pool1_process_radtags/XY1", and the script will append trailing parts as necessary.

I also added quoting to prevent possible problems in case of file names with spaces or special characters.

for fw1file in ~/work/RADseq/Pool1_process_radtags/*.1.fw
do
    samples="${fw1file%.1.fw}"
    /usr/local/bioinfo/src/BBMap/bbmap_38.95/reformat.sh in="$samples".1.fq in2="$samples".2.fq out="$samples".1-reform.fq out2="$samples".2-reform.fq addslash=true
done

(You can use https://www.shellcheck.net to find errors in shell scripts.)

huangapple
  • 本文由 发表于 2023年5月10日 18:53:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217540.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定