LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 12-22-2005, 11:51 PM   #1
Joseph Schiller
Member
 
Registered: Oct 2003
Location: New Jersey
Posts: 44

Rep: Reputation: 15
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
 
Old 12-23-2005, 01:30 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
  


Reply


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
gdb "not in executable format: File format not recognized" tcma Programming 9 07-18-2007 07:02 AM
Realtek 8180 ,Debian, "Invalid module format" medo Linux - Wireless Networking 0 05-25-2005 08:08 AM
KDE 3.2 Print Problem "File Format is not directly supported" forgox Debian 0 09-22-2004 10:07 PM
"Invalid module format" after FULL kernel recompile RedDwarf Linux - General 6 08-07-2004 08:46 PM
"Invalid module format" when I try to install my ATI drivers freddurgan Linux - Newbie 2 05-21-2004 11:49 PM

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

All times are GMT -5. The time now is 11:07 PM.

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