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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
12-22-2005, 10:51 PM
|
#1
|
Member
Registered: Oct 2003
Location: New Jersey
Posts: 44
Rep:
|
how do I use "format" with "pipe" to format decimals
How can I beautify decimals using "format" or "printf" and "pipe" | command with bash?
For example, I have INTC,12/21/2005,25.8,26.1,25.67,25.81,45877677 as ascii in a text file some_file.txt. I can easily "grep -h intc,12/21/2005 ~/some_file.txt | cut -d, -f2,3,4,5,6", but I want to beautify the output to two decimal places. It's "$..f2" as in printf, but I can't seem to make it work with piped-in input. Formatting a string is easy, I've been told, like pulling on a little innocent string that sticking out at the edge of a seam. Do I need vars like String s = "INTC,..." before I can format? There must be an easier way. I can think of sed and big-guns scripts come to mind.
joe
|
|
|
12-23-2005, 12:30 PM
|
#2
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
Hi,
I don't think it is possible to do output formatting using cut, but awk could do this:
grep -h INTC,12/21/2005 some_file.txt | awk -F, '{printf("%s,%.2f,%.2f,%.2f,%.2f\n",$2,$3,$4,$5,$6)}'
Output:
12/21/2005,25.80,26.10,25.67,25.81
And here's an example using awk only (more resource friendly):
awk 'BEGIN { FS=","} /INTC,12\/21\/2005/ { printf("%s,%.2f,%.2f,%.2f,%.2f\n",$2,$3,$4,$5,$6) }' some_file.txt
Output, using your input line:
12/21/2005,25.80,26.10,25.67,25.81
Hope this helps.
|
|
|
All times are GMT -5. The time now is 11:04 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|