0

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>

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
wjandrea
  • 28,235
  • 9
  • 60
  • 81
vicky20
  • 3
  • 3
  • 1
    In one example, you compute the hash of `test1.txt`. In another you compute the sum of `test.txt` and attempt to write to that same file. Are you trying to write to the file that you are computing the sum of? Or is that just a typo? – William Pursell Jul 06 '22 at 16:26
  • Beside the point, but `echo` shouldn't be piped here, since it doesn't produce any output and md5sum doesn't take any input on stdin. It should be connected with `&&` instead. – wjandrea Jul 06 '22 at 16:29
  • Related: [How can I 'echo' out things without a newline?](/q/38021348/4518341) – wjandrea Jul 06 '22 at 16:31
  • No its not a typo. I have only file which test.txt. Say I have 100 lines in the file. In the 101st line I need to write File_Checksum: adbch345wjlfjsafhals – vicky20 Jul 06 '22 at 16:34

4 Answers4

0
printf "%s: %s\n" "File_Checksum:" "$(md5sum < test1.txt | cut ...)" > test.txt

Note that if you are trying to compute the hash of test.txt(the same file you are trying to write to), this changes things significantly.

Another option is:

{
    printf "File_Checksum: "
    md5sum ...
} > test.txt

Or:

exec > test.txt
printf "File_Checksum: "
md5sum ...

but be aware that all subsequent commands will also write their output to test.txt. The typical way to restore stdout is:

exec 3>&1
exec > test.txt # Redirect all subsequent commands to `test.txt`
printf "File_Checksum: "
md5sum ...
exec >&3  # Restore original stdout
William Pursell
  • 204,365
  • 48
  • 270
  • 300
0

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.)

chepner
  • 497,756
  • 71
  • 530
  • 681
0

You can use command grouping to have a list of commands executed as a unit and redirect the output of the group at once:

{ printf 'File_Checksum: ';  md5sum test1.txt | cut -f 1 -d " " } >> test.txt
jschpmr
  • 98
  • 5
-1

Operator && e.g. mkdir example && cd example

  • I think you misunderstood what OP's asking. I just edited the question to make it clearer. – wjandrea Jul 06 '22 at 16:28
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 07 '22 at 17:07