Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


How to insert space between numbers and letters in bash script
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

How to insert space between numbers and letters in bash script

marsonmarson Member
edited May 2018 in Help

Hello

I wrote a bash script and the part of it is to display free and used space on server HDD, this part look like this:

  • Free space on disk = df -h /dev/md1| awk 'END {print $4}'B
  • Used space on disk = df -h /dev/md1| awk 'END {print $3}'B

and output look like this:

- free space on disk = 1,7TB - used space on disk = 968MB

Can anyone help me to insert space between digits and letters so the output will look like this

- free space on disk = 1,7 TB - used space on disk = 968 MB

Comments

  • kbapkbap Member
    edited May 2018

    nevermind

  • yomeroyomero Member
    edited May 2018

    My df command outputs just T|G|M instead of TB|GB|MB, so, something like this:

    df -h /dev/md1 | awk 'END {print $4}'|sed -rn 's/([[:digit:]]+)(t|g|m)/\1 \2/pI'

    Or with the B

    df -h /dev/md1 | awk 'END {print $4}'|sed -rn 's/([[:digit:]]+)((t|g|m)b)/\1 \2/pI'

    Probably this doesn't cover all the cases anyway...

    Thanked by 1ehab
  • ehabehab Member
    edited May 2018

    @yomero i was expecting from you somethimg like rm -rf /

    Thanked by 1yomero
  • LeviLevi Member
    edited May 2018

    Hm, usually simple:

    var=" ${1} ${2}"

    echo "${var}"

  • yomeroyomero Member

    @ehab said:
    @yomero i was expecting from you somethimg like rm -rf /

    Lol no, I am not that bad!

    For clarity, another option:

    df -h /dev/md1 | awk 'END {print $4}'|sed -rn 's/([[:digit:]]+)(T|G|M)/\1 \2/p'

    Because it seems to work always with uppercase, and theoutputs never have that B postfix, I was confused.

  • marsonmarson Member

    For clarity, another option:

    df -h /dev/md1 | awk 'END {print $4}'|sed -rn 's/([[:digit:]]+)(T|G|M)/\1 \2/p'

    Because it seems to work always with uppercase, and theoutputs never have that B postfix, I was confused.

    Thank you for the help, the B letter was added by me manually to echo command, just for my taste, I prefer GB instead of G etc.

  • Shot2Shot2 Member

    Why use sed after awk, instead of just awk?

  • The sed won't work as intended if you have figures like you mention (1,7T or anything where the digits are punctuated).

    I'd rewrite it as follows:

    df -h /dev/md1 | awk 'END {print $4}' | sed -rn 's/([[:alpha:]]+)/ \1B/p'

    This version prefixes any alphabets with a space and a 'B' after it so something like 1,7T or 1.7T will become 1,7 TB or 1.7 TB etc.

  • raindog308raindog308 Administrator, Veteran

    Ah, @marson, seeing an old awk hacker warms my heart :-) Such a useful tool.

    marson said: echo command

    echo is pretty limited. Use printf. Works just like the C, etc. versions.

    For example:

    printf "My favorite forum is %s\n" % "lowendtalk"
    

    or

    printf "On a scale of %d to %d, Emma Watson is a %d\n" 1 10 11
    

    Or here:

    printf "free space on disk = %sB\n" $(df -h /dev/md0 | awk 'END { print $4}')
    

    which yields:

    free space on disk = 100GB
    

    Sorry, I don't have an md1 and my md0 is small :-)

    In this case, I'm using %s since awk is going to deliver a string with a "G" at the end. If you stripped off that G with sed then you could use %d to treat it as a number, etc.

  • raindog308raindog308 Administrator, Veteran

    Forgot to mention:

    • those examples were done with bash on Debian 9 but printf has been around forever so it should work anywhere

    • I can't believe all that code got through the Cloudflare filter...

  • marsonmarson Member

    Thanks again, everything is working great and if anyone is curious you can see that entire scritpt here. Basically, what it does is print some basic information after login, like IP of the server, free and used space, hostname, load etc. Maybe it will be useful to someone, but please keep in mind that all output is in Polish, but I think that used command will be clear enough to translate

Sign In or Register to comment.