The following will convert a string of given numbers to proper format in terms of ensuring numbers are properly separated by a
,, though it won’t add the
$ or round decimals up or down.
echo "1234567890" | \
sed -r '
echo "1234567890.99" | \
sed -r '
That being said, SED really isn’t the best option when it comes to formatting. You could easily achieve the same using any programming language without the need for regex (in most cases).
For example, with PHP, to format a number, you’d simply provide it to the
$number = "12100.99";
echo number_format( $number ) . PHP_EOL;
echo number_format( $number, 2 ) . PHP_EOL;
. PHP_EOL is present to return output on a new line.
The first version will automatically round the given number up or down while the second version tells the function that we want to keep 2 decimal places (and not round).