C
CrazedNerd
Guest
LMFAO, how humiliating...when i wrote that script, i didn't think you could do flow control in sed...but not only can you do flow control in sed, there's a MUCH simpler way to put commas in large numbers:Here's a sed script that takes large numbers (from the thousands to anything less than ten trillion) and puts
commas where they should be:
Code:#trillions s/\([0-9]\)\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)/\1,\2,\3,\4,\5/g #hundred billions s/\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)/\1,\2,\3,\4/g #ten billions s/\([0-9]\{2\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)/\1,\2,\3,\4/g #billions s/\([0-9]\)\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)/\1,\2,\3,\4/g #hundred millions s/\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)/\1,\2,\3/g #ten millions s/\([0-9]\{2\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)/\1,\2,\3/g #millions s/\([0-9]\)\([0-9]\{3\}\)\([0-9]\{3\}\)/\1,\2,\3/g #hundred thousands s/\([0-9]\{3\}\)\([0-9]\{3\}\)/\1,\2/g #ten thousands s/\([0-9]\{2\}\)\([0-9]\{3\}\)/\1,\2/g #one thousands s/\([0-9]\)\([0-9]\{3\}\)/\1,\2/g
I got the idea to do something like this because in a couple of the books i've read, the authors mention ways to put commas between large numbers, but i haven't seen anything that would apply that to anything that comes from the command line output:
Code:xarathustra@xarathustra:~$ df | sed -f easy-numbers.sed Filesystem 1K-blocks Used Available Use% Mounted on tmpfs 1,565,544 3,292 1,562,252 1% /run /dev/nvme0n1p2 805,437,640 421,062,300 343,387,900 56% / tmpfs 7,827,708 23,812 7,803,896 1% /dev/shm tmpfs 5,120 4 5,116 1% /run/lock /dev/nvme0n1p1 523,248 5,364 517,884 2% /boot/efi tmpfs 1,565,540 148 1,565,392 1% /run/user/1,000 /dev/nvme0n1p4 1,093,885,756 5,293,096 1,032,952,596 1% /media/xarathustra/4f763,174-e291-489a-9b8d-e418f7b70fa1 /dev/nvme0n1p3 19,982,160 15,252,016 3,689,760 81% /media/xarathustra/9d485,139-229e-4f36-ade2-d
Of course, it jumbles the columns from thedf
program, so you would need some code from awk to make this look neater...but my script does work to accomplish its intention.
Unix Sed Tutorial: 6 Examples for Sed Branching Operation
This article is part of the on-going Unix Sed Tips and Tricks series. Like any other programming language, sed also provides special branching commands to control the flow of the program. In this article, let us review following two types of Sed branching. Sed Unconditional Branch Sed...
www.thegeekstuff.com
Even the code that page is more verbose that it needs to be:
Code:
:loop
s/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/
t loop
Compare that to the absurd amout of typing i did in my script above Interestingly enough though, the script i made [in the last post] works for quadrillions as well even though i didn't specify a number with that many zeroes in the script, i wonder why...