I know I can replace a substring with sed and integrate command evaluation with $(...). But what if I want the replacement to be calculated from the string to be replaced?


I have a formatted string from stat:

❯ stat --format '%A size %s, birth %.10w mod %.10y' Ambient/The\ XX\ -\ Intro.mp3
-rw-r--r-- size 5126226B, birth 2021-12-05 mod 2021-11-15

And now I want to make the size human-readable. I could of course use another invocation of stat or an intermediary variable:

❯ stat --format '%A size |, birth %.10w mod %.10y' Ambient/The\ XX\ -\ Intro.mp3 | sed "s/|/$(stat --format "%s" Ambient/The\ XX\ -\ Intro.mp3 | numfmt --to=iec)/"
-rw-r--r-- size 4,9M, birth 2021-12-05 mod 2021-11-15


Is there a way to handle this processing within the pipe, maybe through awk or using cut and merging them back together?

  • If you put a space before the comma trailing the size, numfmt should be able to do it for you: stat --format '%A size %s , birth %.10w mod %.10y' file | numfmt --field=3 --to=iec
    – muru
    Dec 7, 2021 at 12:06
  • @muru, remove comma from stat format and numfmt --field=3 --suffix=, --to=iec
    – nezabudka
    Dec 7, 2021 at 12:59
  • 2
    Not the problem you're asking about but - quote strings in shell, don't leave them unquoted and then escape all of the characters you don't want the shell to interpret, e.g. write 'Ambient/The XX - Intro.mp3', not Ambient/The\ XX\ -\ Intro.mp3. See mywiki.wooledge.org/Quotes.
    – Ed Morton
    Dec 7, 2021 at 13:30
  • 1
    It's best to post clean code for us to help you with so we don't see and comment on issues you already know about.
    – Ed Morton
    Dec 7, 2021 at 15:49
  • 1
    If you don't tag the person you're talking to, e.g. @nezabudka, in your comment then they won't be notified and chances are they'll never know you were trying to talk to them.
    – Ed Morton
    Dec 8, 2021 at 16:54

1 Answer 1


If you avoid inserting the B (stat on Ubuntu does not automatically insert this character, so I'm not sure where it comes from) and the comma after the size, you can pipe the output of stat to numfmt and have it convert the third field in its input:

$ stat --format '%A size %s birth %.10w mod %.10y' box.ova
-rw------- size 17098132480 birth - mod 2021-12-06
$ stat --format '%A size %s birth %.10w mod %.10y' box.ova | numfmt --to=si --field=3
-rw------- size         18G birth - mod 2021-12-06

Squeeze multiple consecutive spaces with tr:

$ stat --format '%A size %s birth %.10w mod %.10y' box.ova | numfmt --to=si --field=3 | tr -s ' '
-rw------- size 18G birth - mod 2021-12-06
  • instead of the tr utility use the -d' ' flag and a comma can be added --suffix=, in numfmt
    – nezabudka
    Dec 8, 2021 at 19:50
  • Interesting, in stat format it work grouping "%'s"
    – nezabudka
    Dec 8, 2021 at 20:58
  • This was personally my final command :) stat --format '%A %s - birth %.10w mod %.10y' "$arg" | numfmt --field=2 --to=iec-i --padding=6 --suffix=B
    – xeruf
    Dec 9, 2021 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.