LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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,752

Rep: Reputation: 463Reputation: 463Reputation: 463Reputation: 463Reputation: 463
"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 11.4
Posts: 1,319

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: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
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.
 
  


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 03:43 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration