LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 02-26-2011, 09:29 AM   #16
turtlegeek
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:
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
 
1 members found this post helpful.
Old 02-26-2011, 09:52 AM   #17
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,803

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

Rep: Reputation: 231Reputation: 231Reputation: 231
turtlegeek,

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


Quote:
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, 11:26 AM   #19
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 13.1
Posts: 1,323

Rep: Reputation: 252Reputation: 252Reputation: 252
Quote:
Originally Posted by turtlegeek View Post
Reuti,

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:
Code:
$ wget ftp://ftp.gnu.org/gnu/sed/sed-4.2.1.tar.gz
--2011-02-26 18:07:39--  ftp://ftp.gnu.org/gnu/sed/sed-4.2.1.tar.gz
           => `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, 03: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.
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.
 
Old 03-29-2015, 10:44 AM   #21
Quanswers
LQ Newbie
 
Registered: Sep 2003
Location: Thousand Oaks CA
Distribution: CrunchBang 11 "Waldorf"
Posts: 4

Rep: Reputation: 0
Format numbers using bash (more)

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

Last edited by Quanswers; 03-29-2015 at 10:48 AM.
 
Old 03-30-2015, 06:47 AM   #22
rtmistler
Senior Member
 
Registered: Mar 2011
Location: Milford, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 2,525
Blog Entries: 9

Rep: Reputation: 835Reputation: 835Reputation: 835Reputation: 835Reputation: 835Reputation: 835Reputation: 835
Quote:
Originally Posted by Quanswers View Post
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.
 
  


Reply

Tags
sed


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 11:54 AM
Bash + How to add Hexadecimal numbers trscookie Programming 7 09-03-2007 04:22 PM
[BASH] Search for 5-digit numbers in document General Programming 2 01-10-2007 08:06 PM
Decimal numbers in bash script variables? Massif Programming 3 11-07-2005 09:01 PM
fstab file format - numbers at the end jjisnow Linux - Newbie 3 03-23-2004 06:54 PM


All times are GMT -5. The time now is 01:48 AM.

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