Search code examples
checksummd5sum

md5sum - ignore trailing whitespace


Say I have two files:

1.json

{"foo":"bar"}\n

2.json

{"foo":"bar"}

when using checksum routines, is there a way to ignore the trailing whitespace?

maybe something like this:

md5sum < <(cat file | trim_somehow)

Solution

  • You can use sed or xargs.

    xargs is much simpler, but be careful with it. I'm not sure if it is safe to use in this context. Read the comments below this answer https://stackoverflow.com/a/12973694/4330274. (There are a lot of answers to your question in that post).

    md5sum < <(cat file | xargs) will delete trailing/leading whitespaces (Also, as stated by dave_thompson_085 on the comments down below, it will compress each whiltespace sequence to one whitespace and it will remove quotation marks and backslashes) from the file before passing it to the md5sum utility.

    Note: xargs appends a new line to the end of the input.

    I recommend using sed for this purpose. It is much safer. Read this answer https://stackoverflow.com/a/3232433/4330274