Bash:在目录中排序,将文件内容和新文件中的最小条目连接起来
我有一组RUN1、RUN2等目录 在每个目录中,都有一组文件。在每个文件中,有两个数字。例如(这些文件保存为.csv,尽管此处显然没有逗号): 等等 我想这样做:Bash:在目录中排序,将文件内容和新文件中的最小条目连接起来,bash,Bash,我有一组RUN1、RUN2等目录 在每个目录中,都有一组文件。在每个文件中,有两个数字。例如(这些文件保存为.csv,尽管此处显然没有逗号): 等等 我想这样做: For each directory: For each file in a directory: Sort files by the first entry Save contents and path of file with smallest value of first entry.
For each directory:
For each file in a directory:
Sort files by the first entry
Save contents and path of file with smallest value of first entry.
例如,如上所述,这将生成一个新文件,其中包含:
2.32e-00 1.2e-01 ./RUN1/mod_1.csv
1.23e-01 0.5e-02 ./RUN2/mod_a.csv
我一开始是这样做的:
#!/bin/bash
resultfile="best_results.txt"
for d in $(find . -type d -name 'RUN*' | sort);
do
find "$d" -type f -name 'mod*' -exec awk '{print $0, FILENAME}' {} \; >> "$resultfile"
done
但它给出了所有文件中的两个值,如下所示:
2.32e-00 ./RUN1/mod_1.csv
1.2e-01 ./RUN1/mod_1.csv
4.53e-00 ./RUN1/mod_b.csv
1.1e-01 ./RUN1/mod_b.csv
1.23e-01 ./RUN2/mod_a.csv
0.5e-02 ./RUN2/mod_a.csv
1.67e-00 ./RUN2/mod_3.csv
0.4e-01 ./RUN2/mod_3.csv
2.32e-00 1.2e-01 ./RUN1/mod_1.csv
1.23e-01 0.5e-02 ./RUN2/mod_a.csv
然后我想我需要使用head
,但是这个修改:
find "$d" -type f -name 'mod*' -exec awk '{print $0, FILENAME}' {} \; | head -1 >> "$resultfile"
给我:
find: `awk' terminated by signal 13
我想我需要另一个排序
,可能还需要头
,但我不能把它们放在一起
编辑(为清晰起见):
我想查看目录中的所有文件,找到第一个编号最小的文件,然后将该文件的值和文件路径写入新文件。然后,转到下一个目录并执行相同的操作。在我的例子中:
目录RUN1
包含文件mod_1.csv
和mod_b.csv
。文件mod_1.csv
的第一个值最小。我想将其内容和文件路径写在一行上:
2.32e-00 1.2e-01 ./RUN1/mod_1.csv
1.23e-01 0.5e-02 ./RUN2/mod_a.csv
归档
目录RUN2
包含文件、mod_a.csv
和mod3.csv
。文件mod_a.csv
的第一个值最小。我想将其内容和文件路径写在一行上:
2.32e-00 1.2e-01 ./RUN1/mod_1.csv
1.23e-01 0.5e-02 ./RUN2/mod_a.csv
因此,新文件如下所示:
2.32e-00 ./RUN1/mod_1.csv
1.2e-01 ./RUN1/mod_1.csv
4.53e-00 ./RUN1/mod_b.csv
1.1e-01 ./RUN1/mod_b.csv
1.23e-01 ./RUN2/mod_a.csv
0.5e-02 ./RUN2/mod_a.csv
1.67e-00 ./RUN2/mod_3.csv
0.4e-01 ./RUN2/mod_3.csv
2.32e-00 1.2e-01 ./RUN1/mod_1.csv
1.23e-01 0.5e-02 ./RUN2/mod_a.csv
我知道这在我的编辑前问题中并不清楚。请提出任何问题!我不知道如何更清楚地说明这一点。您可能希望在查找过程中删除换行符:
resultfile="best_results.txt"
for d in $(find . -type d -name 'RUN*');
do
find "$d" -type f -name 'mod*' -exec awk '{printf "%s ",$0} END {print "", FILENAME}' {} \;
done | sort >> "$resultfile"
排序通常会在最后完成(一旦所有结果都从stdout返回),但是,不清楚您希望如何对其进行排序。如果您真的愿意,您可能可以去掉for循环
,因为使用类似于以下内容的东西应该也能起到类似的作用:
find RUN* -type f -name 'mod*' -exec awk '{printf "%s ",$0} END {print "", FILENAME}' {} \; | sort -k 2,2 >> "$resultfile"
将-k
选项与排序
一起使用,以指定要排序的列
结果(使用排序-k 2,2):
是的,我希望它们在同一行上。每个文件只有2行或更多行吗?每个文件只有2行,如示例中所示。这并不能回答问题,因为我只希望每个目录中有一个文件。这有两个。@StatsSorceress:你的问题不清楚,请编辑。@I'L'I更好吗?