简体   繁体   English

Bash 脚本 shell:如何将文件名对应的变量用于嵌套 for 循环

[英]Bash script shell : how to use variable corresponding to filename into nested for loop

I have a large number of directories with their names as:我有大量目录,其名称为:

Cl_GG_Omega_m_step_1e-01_der_15
Cl_GG_Omega_m_step_2e-01_der_15
Cl_GG_Omega_m_step_2e-01_der_15
Cl_GG_Omega_m_step_1e-08_der_15
Cl_LL_Omega_m_step_1e-08_der_15
Cl_GL_Omega_m_step_1e-08_der_15
Cl_GG_Omega_m_step_1.1229481712787273e-08_der_15
Cl_LL_Omega_m_step_1.1229481712787273e-08_der_15
Cl_GL_Omega_m_step_1.1229481712787273e-08_der_15
Cl_GG_Omega_m_step_1.2610125953782405e-08_der_15
Cl_LL_Omega_m_step_1.2610125953782405e-08_der_15

I would like to get the directory name (full) whose value appearing between Òmega_m_step and _der_15 is below a cutoff (for example cutoff=0.01 below):我想获取出现在Òmega_m_step_der_15之间的值低于截止值的目录名称(完整)(例如下面的cutoff=0.01 ):

For this, I tried to do:为此,我尝试这样做:

for i in $(ls --color=never -d Cl_*Omega_m_step_*); do
    for j in $(echo $i); do
        sed 's/.*_step_\([0-9].[0-9]*\)_der_15/\1/g' "$j" | awk -v cutoff=0.01 '{if ($1 < cutoff) print "$j"}'; 
    done;
done

The difficulty here is that I would like to get the entire directory name, that's why I used a double for loop: the issue is that I don't know to get into nested loop the $j directory name (coming from for j in $(echo $i) , especially printing it from awk -v cutoff=0.01 '{if ($1 < cutoff) print "$j"}' command?这里的困难是我想获取整个目录名称,这就是我使用双for循环的原因:问题是我不知道要进入嵌套循环$j目录名称(来自for j in $(echo $i) ,尤其是从awk -v cutoff=0.01 '{if ($1 < cutoff) print "$j"}'命令打印它?

Update更新

Thanks for your answers, it helped me a lot.谢谢你的回答,对我帮助很大。 Just a last thing, for the fun, how can I use with Shell bash (I mean that I make reference) the outer index $i into nested loop of a double loop, ie:最后一件事,为了好玩,我如何使用Shell bash (我的意思是我参考)外部索引 $i 到双循环的嵌套循环中,即:

for i in $(...); do for j in $(...); do "reference to i"; done; done

Can I do a simple $i or "$i" ?我可以做一个简单的$i"$i"吗? You will tell me of course that it will depend on the command that uses this index in nested loop.您当然会告诉我,这将取决于在嵌套循环中使用此索引的命令。 So, even a simple case would be enough for me.所以,即使是一个简单的案例对我来说也足够了。

As noted by the comments, the file name can be broken into '_' separated tokens, including the step attribute (6th column).正如评论所指出的,文件名可以分解为“_”分隔的标记,包括step属性(第 6 列)。 Awk can be used to apply a numeric filter. Awk 可用于应用数字过滤器。

ls Cl_*Omega_m_step_* | awk -F_ '$6<0.01'

Per OP question, clarification for awk programs: the following are equivalent.每个 OP 问题,对awk程序的说明:以下是等效的。

condition 
condition { print }
{ if ( condition ) print }

For this example:对于这个例子:

awk -F_ '$6<0.01'
awk -F_ '$6<0.01 { print }'
awk -F_ '{ if ($6<0.01) print }'

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM