Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Concatenate the output of 2 commands in the same line in Unix

I have a command like below

md5sum test1.txt | cut -f 1 -d " " >> test.txt

I want output of the above result prefixed with File_CheckSum:

Expected output: File_CheckSum: <checksumvalue>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried as follows

echo 'File_Checksum:' >> test.txt | md5sum test.txt | cut -f 1 -d " " >> test.txt

but getting result as

File_Checksum:
adbch345wjlfjsafhals

I want the entire output in 1 line

File_Checksum: adbch345wjlfjsafhals

>Solution :

echo writes a newline after it finishes writing its arguments. Some versions of echo allow a -n option to suppress this, but it’s better to use printf instead.

You can use a command group to concatenate the the standard output of your two commands:

{ printf 'File_Checksum: '; md5sum test.txt | cut -f 1 -d " "; } >> test.txt

Note that there is a race condition here: you can theoretically write to test.txt before md5sum is done reading from it, causing you to checksum more data than you intended. (Your original command mentions test1.txt and test.txt as separate files, so it’s not clear if you are really reading from and writing to the same file.)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading