ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
My Mac sed does have an option similar to -r, it's -E:
This works for my PowerMac running Mac OS Tiger 10.4.11:
Code:
echo " 1234567890 " | \
sed -E '
: L
s=([0-9]+)([0-9]{3})=\1,\2=
t L'
But I must say that this version of sed does not accept semicolons (
in place of newlines.
Per archtoad's comments, 1,10,11: it's my pleasure. 2 can be answered above.
3. This will happen soon, hopefully.
4. Separate lines were necessary for me but you did improve the entry!
5. True but a good illustration of the use of \b.
6. My habit to put the -e option there is a mnemonic device to curb my zealousness!
7. I found the use of '=' as a delimiter to be original (to me) and instructive.
My use of / delimiters here is my custom derived from years of vi use and man reading.
I find slashes and vertical bars are visually helpful when reading replacement commands.
it's probably redundant here but the man page said it well:
"
Any character other
than backslash or newline can be used instead of a slash to
delimit the RE and the replacement. Within the RE and the
replacement, the RE delimiter itself can be used as a literal
character if it is preceded by a backslash.
"
8. "[0-9]+" is not allowed by my version of BRE. On impulse, I tried using the asterisk ("*")
but it caused sed to spin. "[0-9]{4,19}" leaves out the first comma in the example number.
9. "g", like my -e is unnecessary but did no damage either.
Reuti,
Good suggestion. I could not justify the trouble to learn the compile environment
until I saw the code from archtoad.
Last edited by turtlegeek; 02-27-2011 at 04:11 PM.
Reason: use code block
This Bash function also adds thousands separators. It is string-based, so it assumes the value is an integer. If the value contains an odd number of negative signs "-" (anywhere), it will be output as negative.
Code:
# Usage: printnum [-n] [-e] [-s,] value
printnum () {
local separator=","
local options=()
while [ $# -gt 0 ]; do
if [ "$1" == "-n" ]; then
options=("${options[@]}" "-n")
shift 1
elif [ "$1" == "-e" ]; then
options=("${options[@]}" "-e")
shift 1
elif [ "${1:0:2}" == "-s" ]; then
separator="${1:2}"
shift 1
else
break
fi
done
local number="${1//[^0123456789]/}"
local prefix="${1//[^-]/}"
local prefix=$[ (${#prefix} -0) % 2]
if [ $prefix -gt 0 ]; then
local prefix="-"
else
local prefix=""
fi
local suffix=""
shift 1
while [ ${#number} -gt 3 ]; do
local index=$[${#number} -3]
if [ ${#suffix} -gt 0 ]; then
suffix="${number:$index}$separator$suffix"
else
suffix="${number:$index}"
fi
number="${number:0:$index}"
done
if [ ${#number} -gt 0 ]; then
if [ ${#suffix} -gt 0 ]; then
suffix="$number$separator$suffix"
else
suffix="$number"
fi
fi
echo "${options[@]}" "$prefix$suffix"
}
This function can easily be extended to parse multiplier suffixes (k, M, G, T, P), and/or to output an optional decimal part from a separate argument, even with optional divisor suffixes (d,c,m,u/µ,n,p,f). It's not as good as locale conversion, but should suffice for shell scripts.
I'm using CrunchBang 11 "Waldorf" (derived from Debian 7 "Wheezy")
There's a BASH command called 'rev' that reverses characters in
a line, thus simplifying the work sed has to do.
(the last sed gets rid of any leading comma's that might occur)
$ echo 123456 | rev | sed -r 's/([0-9]{3})/\1,/g' | rev | sed 's/^,//'
123,456
$ echo 12345678901234567890 | rev | sed -r 's/([0-9]{3})/\1,/g' | rev | sed 's/^,//'
12,345,678,901,234,567,890
I'm using CrunchBang 11 "Waldorf" (derived from Debian 7 "Wheezy")
There's a BASH command called 'rev' that reverses characters in
a line, thus simplifying the work sed has to do.
(the last sed gets rid of any leading comma's that might occur)
$ echo 123456 | rev | sed -r 's/([0-9]{3})/\1,/g' | rev | sed 's/^,//'
123,456
$ echo 12345678901234567890 | rev | sed -r 's/([0-9]{3})/\1,/g' | rev | sed 's/^,//'
12,345,678,901,234,567,890
Just a heads up to note that you've resurrected a very old post, greater than 4 years since last prior post.
The forum should have warned you about that fact to alert you that maybe continuing the discussion is not too germane.
Looks like you were wishing to add to the answer. Genial enough, but I'm guessing that it might not benefit the original poster any longer.
In the event where you find a similar thread but have a continued question, I realize that's not the case here, but a style to consider would be to refer to the much older thread with an entirely new thread to present a current problem/question.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.