Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


Search this Thread
Old 02-26-2011, 10:29 AM   #16
LQ Newbie
Registered: Feb 2011
Location: Georgia, USA
Distribution: xubuntu, Mac OS X
Posts: 5

Rep: Reputation: 16

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:
echo " 1234567890 " | \
sed -E '
: L
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.


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 05:11 PM. Reason: use code block
1 members found this post helpful.
Old 02-26-2011, 10:52 AM   #17
Amigo developer
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,783

Rep: Reputation: 482Reputation: 482Reputation: 482Reputation: 482Reputation: 482
"So I spent a few minutes" -from the date of the thead it seems more like a couple of years...
Old 02-26-2011, 11:51 AM   #18
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231

Nice post, I'll give you rep for it when I'm allowed to again.

Originally Posted by gnashley View Post
"So I spent a few minutes" -from the date of the thead it seems more like a couple of years...
Ho :-], HO , HO
Old 02-26-2011, 12:26 PM   #19
Senior Member
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 13.1
Posts: 1,320

Rep: Reputation: 252Reputation: 252Reputation: 252
Originally Posted by turtlegeek View Post

Good suggestion. I could not justify the trouble to learn the compile environment
until I saw the code from archtoad.
When you install Xcode, you also get the gcc installed. To compile sed then in a different version is like:
$ wget
--2011-02-26 18:07:39--
           => `sed-4.2.1.tar.gz'
$ tar -xf sed-4.2.1.tar.gz
$ cd sed-4.2.1
$ ./configure --prefix=$HOME/local/sed-4.2.1
$ make
$ make install
$ cd
$ export PATH=$HOME/local/sed-4.2.1/bin:$PATH
You can download also with the browser, as I installed wget also afterwards as its not included in the Mac by default
Old 02-27-2011, 04:38 PM   #20
Nominal Animal
Senior Member
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943
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.
# 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
            shift 1
    local number="${1//[^0123456789]/}"
    local prefix="${1//[^-]/}"
    local prefix=$[ (${#prefix} -0) % 2]
    if [ $prefix -gt 0 ]; then
        local prefix="-"
        local prefix=""
    local suffix=""
    shift 1
    while [ ${#number} -gt 3 ]; do
        local index=$[${#number} -3]
        if [ ${#suffix} -gt 0 ]; then
    if [ ${#number} -gt 0 ]; then
        if [ ${#suffix} -gt 0 ]; then
    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.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Format large numbers with commas at every thousandth decimal spot helpmhost Linux - General 8 10-01-2007 12:54 PM
Bash + How to add Hexadecimal numbers trscookie Programming 7 09-03-2007 05:22 PM
[BASH] Search for 5-digit numbers in document General Programming 2 01-10-2007 09:06 PM
Decimal numbers in bash script variables? Massif Programming 3 11-07-2005 10:01 PM
fstab file format - numbers at the end jjisnow Linux - Newbie 3 03-23-2004 07:54 PM

All times are GMT -5. The time now is 12:00 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration