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.
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
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
nevermind
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...
@yomero i was expecting from you somethimg like
rm -rf /
Hm, usually simple:
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.
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.
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.
Ah, @marson, seeing an old awk hacker warms my heart :-) Such a useful tool.
echo is pretty limited. Use printf. Works just like the C, etc. versions.
For example:
or
Or here:
which yields:
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.
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...
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