【发布时间】:2017-03-06 18:58:25
【问题描述】:
我有一组目录:
RUN1 RUN2 RUN3
在每个目录中,我都有文件。 RUN1 有:
mod1_1 mod1_2 mod1_3
而 RUN2 有:
mod2_1 mod2_2 mod2_3
等等
每个文件都有这样的行(这是 mod1_1):
8.69e-01 2.59e-01 7.82e-01 4.92e-01
8.69e-01 2.56e-01 7.84e-01 4.95e-01
8.72e-01 2.54e-01 7.83e-01 5.00e-01
8.71e-01 2.53e-01 7.84e-01 5.01e-01
8.73e-01 2.53e-01 7.81e-01 4.99e-01
这是 mod1_2:
8.69e-01 2.59e-01 7.82e-01 4.98e-01
8.69e-01 2.56e-01 7.84e-01 4.90e-01
8.72e-01 2.54e-01 7.83e-01 5.00e-01
8.71e-01 2.53e-01 7.84e-01 5.01e-01
8.73e-01 2.53e-01 7.81e-01 4.99e-01
我想为每个 mod 文件创建一个仅包含第 4 列中最小数字的新文件。例如,假设 mod1_1 和 mod2_1 是唯一的文件。我想创建一个新文件,其中包含来自 mod1_1 的第 1 行和来自 mod2_1 的第 2 行:
8.69e-01 2.59e-01 7.82e-01 4.92e-01
8.69e-01 2.56e-01 7.84e-01 4.90e-01
我想为每个 RUN 目录执行此操作。我试过这个:
#/bin/bash
finddir=$(find -type d -name 'RUN*' | sort) #find the dirs
for i in $finddir; do
cd $i
echo $(pwd)
findfiles=$(find -type f -name 'mod*' | sort -V) #find the files
echo $findfiles
for j in $findfiles; do
s1=$(sort -k3,3 j)
echo $s1
done
我的问题是排序命令,我不知道如何将结果写入文件。有任何想法吗?
伪代码以防万一:
For each directory RUN*
For each file mod*
get the minimum value in column 4, save the line that has that value
End for
Write the lines that had the minimum values to a new file
End for
编辑:仍然有问题。以下是我的修改方式:
#/bin/bash
finddir=$(find -type d -name 'RUN*' | sort) #find the dirs
for i in $finddir; do
cd $i
echo $(pwd)
findfiles=$(find -type f -name 'mod*' | sort -V) #find the files
for j in $findfiles; do
s1=$(sort -k 4 -g $j)
echo -n "$s1"
done
cd ..
done
我在错误的部分'cd'。这有点好 - 它给了我每行的四个数字 - 但它不只返回每个文件中第 4 列的最小值的行。另外,我仍然不知道如何将最终结果导出到新文件。
【问题讨论】:
标签: bash