Bash脚本将两行输出合并为一行

时间:2017-07-14 14:32:02

标签: bash sed

我在Windows上有以下bash脚本为文件夹中的文件列表生成MD5哈希:

$ ls -rt | while read -r file; do (()); CertUtil -hashfile "${file}" MD5 >> foo.txt; done;

对于每个文件bar,我得到以下两行:

MD5 hash of file bar:
0ae58a1af151446ac8b283b6e70ea157

我想将输出管道重新格式化为:

0ae58a1af151446ac8b283b6e70ea157   bar

我想这可以用sed完成吗?不知道如何继续。

1 个答案:

答案 0 :(得分:3)

你可以这样做:

$ ls -rt | while read -r file; do echo $(CertUtil -hashfile "${file}" MD5) "${file}" >> foo.txt; done;

但更好的是(safer使用文件名扩展而不是迭代ls输出):

for file in *; do
    echo "$(CertUtil -hashfile "${file}" MD5)   ${file}" >> foo.txt
done

我不确定CertUtil,但是如果你可以安装GNU md5sum,那么完整的脚本可以替换为:

md5sum * >>foo.txt