提取txt中的最后一个元素并保留文件夹名称

时间:2017-10-13 20:53:36

标签: awk

我有40个具有唯一名称的文件夹,每个文件夹中都有一个名为summary.txt的文件,如下所示:

HISAT2 summary stats:
Total reads: 36590175
    Aligned 0 time: 1238197 (3.38%)
    Aligned 1 time: 33866701 (92.56%)
    Aligned >1 times: 1485277 (4.06%)
Overall alignment rate: 96.62%

我想创建一个新的.txt文件,其中包含文件夹名称列,以及“96.62%”列,以便最终结果如下:

Folder name        alignment rate
Sample1             96.62%
Sample2             94.53%
...                 ...
SampleN             96.22%

有没有办法使用命令行执行此操作。也许awk?任何帮助将不胜感激。

哈利

3 个答案:

答案 0 :(得分:0)

Awk 解决方案:

之前的步骤(在result.txt中设置标题行):

$ cat > result.txt
Folder name        alignment rate
awk '/^Overall/{ 
         printf "%-20s%s\n",substr(FILENAME,0,index(FILENAME, "/")-1), $NF >> "result.txt" 
    }' Sample*/summary.txt

result.txt内容应如下所示:

Folder name        alignment rate
Sample1             96.62%
Sample2             94.53%
...

答案 1 :(得分:0)

一个简单的awk脚本:

$ awk -F': ' 'BEGIN { print "folder", "rate" }
              /Overall/ { sub("/.*","",FILENAME); print FILENAME, $2 }' */summary.txt
folder rate
a 96.62%
b 91.63%
c 93.22%

答案 2 :(得分:0)

使用查找命令

$ echo -e "Folder name\talignment rate" > output.txt

$ find . -iname "summary.txt" -exec awk 'END{ match(FILENAME,/\/(\w+)\//,a); print a[1]"\t\t"$4}' {} \; > output.txt

输出:

Folder name     alignment rate
dir1            96.62%
dir2            96.62%