Jacko
Jacko

Reputation: 13195

Bash script to merge two lines of output into one

I have the following bash script on windows to generate MD5 hashes for a list of files in a folder:

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

For each file bar, I get the following two lines:

MD5 hash of file bar:
0ae58a1af151446ac8b283b6e70ea157

I would like to pipe the output to reformat as:

0ae58a1af151446ac8b283b6e70ea157   bar

I suppose this can be done with sed ? Not sure how to proceed.

Upvotes: 0

Views: 55

Answers (1)

randomir
randomir

Reputation: 18697

You can do it like this:

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

But even better would be (safer to use filename expansion than to iterate over ls output):

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

I'm not sure for CertUtil, but if you can install GNU md5sum, the complete script can be replaced with:

md5sum * >>foo.txt

Upvotes: 3

Related Questions