Most of the replies seems a bit hard to grasp when reading them, although most are native shell solutions. I had the same issue as OP and decided to write a super small go binary to do it instead.
The code is straightforward and easy to understand, the binary builds for any OS/arch.