【发布时间】:2020-09-04 04:04:29
【问题描述】:
我有以下directory 结构,其中包含某些感兴趣的文件,我必须使用awk 对其进行计算/算术运算。
$ mkdir DP1/postProcessing/0/ DP2/postProcessing/0/ DP3/postProcessing/0/;
$ touch DP1/postProcessing/0/wallShearStress.dat DP1/postProcessing/0/wallShearStress_0.02.dat DP2/postProcessing/0/wallShearStress_0.dat DP2/postProcessing/0/wallShearStress_0.1.dat DP3/postProcessing/0/wallShearStress_0.05.dat DP3/postProcessing/0/wallShearStress_0.000012.dat
masterDir/;
$ tree masterDir/
masterDir/
├── DP1
│ └── postProcessing
│ └── 0
│ ├── wallShearStress_0.02.dat
│ └── wallShearStress.dat
├── DP2
│ └── postProcessing
│ └── 0
│ ├── wallShearStress_0.1.dat
│ └── wallShearStress_0.dat
└── DP3
└── postProcessing
└── 0
├── wallShearStress_0.000012.dat
├── wallShearStress_0.05.dat
└── wallShearStress.dat
预期输出
DP File_processed Ouput_value #Optional header
DP1 wallShearStress_0.02.dat <some result using AWK>
DP2 wallShearStress_0.1.dat <some result using AWK>
DP3 wallShearStress_0.05.dat <some result using AWK>
我的(非常基本的)尝试失败,脚本只为找到的最后一个目录返回文件三次:
$ for i in $(find -type d -name "DP*"); do
> for j in $(find . -type f -name "wallShearStress*" | tail -n 1); do
> echo $j;
> awk 'NR == 3 {print $0}' $j; # this just for example ...
> # but I wanna do something more here, but no issue with that
> # once I can get the proper files into AWK.
> done;
> done;
./DP3/postProcessing/0/wallShearStress_0.05.dat
./DP3/postProcessing/0/wallShearStress_0.05.dat
./DP3/postProcessing/0/wallShearStress_0.05.dat
问题定义:我想,
- 首先,在每个目录中找到名为
wallShearStress*.dat的文件。在哪里, - 感兴趣的文件应该在结尾处具有最高编号。 (澄清一下,一个目录中存在多个
wallShearStress*.dat文件,例如对于DP3,应该只选择DP3\postProcessing\0\wallShearStress_0.05.dat进行处理,因为它的优先级高于DP3\postProcessing\0\wallShearStress.dat,同样只有DP1\postProcessing\0\wallShearStress_0.02.dat和DP2\postProcessing\0\wallShearStress_0.1.dat应该被选中) - 使用 awk 对所选的
wallShearStress*.dat执行算术运算,对每个目录在masterDir中输出为.txt/.csv文件,如下所示:
问题
- 这种方法有什么问题?
- 有更好的方法吗? (请记住,问题在于获取正确的文件,而不是 AWK)。
【问题讨论】:
-
感谢您指出错误。已更正。
标签: directory awk bash awk bash shell awk directory openfoam