LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 01-19-2009, 09:40 AM   #1
kinetik
Member
 
Registered: Dec 2005
Location: The most beautiful city in the world.
Distribution: Mostly RedHat. Also Suse, Ubuntu, PHLAK etc.
Posts: 149

Rep: Reputation: 15
sed question


Hi all


This question is closely related to this one.

Say I got a value of 15347, how do I get this to display as 15.347 by using sed?

I tried:

sed "s|[0-9][0-9][0-9]$|\.[0-9][0-9][0-9]|g"


That, though, gives me this:

15.[0-9][0-9][0-9]


Any ideas?

Last edited by kinetik; 01-19-2009 at 09:52 AM.
 
Old 01-19-2009, 09:59 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by kinetik View Post
This question is closely related to this one.
You did not post any follow up to that thread, so we don't know if you resolved the issue, following the given suggestions.

Anyway, in sed you have to use a pattern reference. That is you have to embed in escaped parentheses the part of the pattern you want to remember, then use \1 as the first pattern, \2 as the second pattern and so on. Your line should be modified as follows:
Code:
echo 15347 | sed "s/\([0-9][0-9][0-9]$\)/\.\1/g"
For more details about this method, see the must-read Sed - An Introduction and Tutorial.

Regarding your original question in the previous thread: to sum floating point numbers without bc you can use awk (as already suggested by H_TeXMeX_H) or even python (by ErV). In awk I would do something more efficient as:
Code:
awk '{sum+=$0}END{print sum}' file
You can try awk also as alternative to the sed command above:
Code:
echo 15347 | awk '{printf "%6.3f\n", $0/1000}'
this will give more flexibility about the input number and the output format (you can chose how many digits before and after the dot).
 
Old 01-19-2009, 10:05 AM   #3
kinetik
Member
 
Registered: Dec 2005
Location: The most beautiful city in the world.
Distribution: Mostly RedHat. Also Suse, Ubuntu, PHLAK etc.
Posts: 149

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix View Post
You did not post any follow up to that thread, so we don't know if you resolved the issue, following the given suggestions.
Indeed, and I'm terribly sorry about that.
That's my hard-headedness coming out as I already had a sort of idea how I wanted to tackle this without resorting to another language.

I'm crazy about sed, and I really wanted to be able to tackle my first problem by getting this question answered first. Maybe even learn something more about sed in the process (which I just did by reading your advice).

The plan:

Take out the decimal characters, add the numbers together, then just insert the decimal back in three numbers from the end.


No idea if it's going to work, but here's hoping.


Thanks for your help so far as well, much appreciated.
 
Old 01-19-2009, 10:21 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by kinetik View Post
Indeed, and I'm terribly sorry about that.
That's my hard-headedness coming out as I already had a sort of idea how I wanted to tackle this without resorting to another language.
No need to sorry... really. Furthermore I noticed it was a very recent thread, just after I posted to this one.
Quote:
Take out the decimal characters, add the numbers together, then just insert the decimal back in three numbers from the end.
Theoretically it should work. Practically it would be very difficult to take in account the different number of digits in each value using sed. Again I'd suggest a more efficient way using awk, since it can do floating point calculations in a straightforward manner.
 
Old 02-03-2009, 02:53 AM   #5
kinetik
Member
 
Registered: Dec 2005
Location: The most beautiful city in the world.
Distribution: Mostly RedHat. Also Suse, Ubuntu, PHLAK etc.
Posts: 149

Original Poster
Rep: Reputation: 15
Hi colucix


Just want to say thanks again for that brilliant piece of awk script you provided. I'm using it extensively at the moment!
 
Old 02-03-2009, 04:53 AM   #6
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Quote:
Originally Posted by colucix View Post
In awk I would do something more efficient as:
Code:
awk '{sum+=$0}END{print sum}' file
Interesting, I didn't know it could be put on one line like that. Still, it's not that readable, where is sum initialized ?
 
Old 02-03-2009, 05:09 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by H_TeXMeX_H View Post
Interesting, I didn't know it could be put on one line like that. Still, it's not that readable, where is sum initialized ?
Awk initializes variables to 0, if not given a specific value. To be sure, you can always add the begin section:
Code:
awk 'BEGIN{sum=0}{sum+=$0}END{print sum}' file
but it is not really necessary.

Here is the relevant section from the official AWK programming guide (section 5.3.1)
Quote:
Variables in awk can be assigned either numeric or string values. The kind of value a
variable holds can change over the life of a program. By default, variables are initialized to
the empty string, which is zero if converted to a number. There is no need to “initialize”
each variable explicitly in awk, which is what you would do in C and in most other traditional
languages.

Last edited by colucix; 02-03-2009 at 05:31 AM. Reason: Added reference
 
  


Reply



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
Another SED question 3saul Linux - Software 2 01-28-2007 12:06 PM
sed question? slack66 Linux - Newbie 7 07-26-2006 06:47 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
sed question ckoniecny Linux - General 3 11-11-2005 09:31 AM
sed question lazyuser Programming 4 02-11-2005 06:11 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:36 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
Open Source Consulting | Domain Registration