LinuxQuestions.org
Review your favorite Linux distribution.
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-22-2009, 09:51 AM   #1
imkornhulio
Member
 
Registered: Aug 2008
Posts: 54

Rep: Reputation: 15
Edit txt file


I need to edit a text file in shell so that the 3rd number in each line has two decimal points

for example

joe 2456 12
matt 51235 356
tim 12 5.00
jeff 2 3.0

in need it to be

joe 2456 12.00
matt 51235 356.00
tim 12 5.00
jeff 2 3.00

If someone could guide me in right direction i will really appreciate it.

Thanks in advance..

Last edited by imkornhulio; 01-22-2009 at 10:19 AM.
 
Old 01-22-2009, 10:42 AM   #2
imkornhulio
Member
 
Registered: Aug 2008
Posts: 54

Original Poster
Rep: Reputation: 15
I know i have to use awk but dont know on where to start.

Need Help
 
Old 01-22-2009, 10:45 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
You can try to use the printf statement in awk. To specify a floating point with 2 decimal digits, you can try the "%.2f" format.
 
Old 01-22-2009, 10:48 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

It is not clear from your example if the 2 decimal places are always 0 (zero), if not I think AWK is in order:

awk '{printf("%s %d %.2f\n", $1, $2, $3) }' infile

The printf statement is the key, especially the %.2f part. Take a look here (Using AWK), near the bottom of the page for an explanation.

If the 2 decimal places are always the same (say 0) there are other solutions as well (sed comes to mind first).

Hope this helps.
 
Old 01-22-2009, 11:07 AM   #5
imkornhulio
Member
 
Registered: Aug 2008
Posts: 54

Original Poster
Rep: Reputation: 15
Thanks for the help guys.
 
Old 01-22-2009, 12:03 PM   #6
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by imkornhulio View Post
I need to edit a text file in shell so that the 3rd number in each line has two decimal points

for example

joe 2456 12
matt 51235 356
tim 12 5.00
jeff 2 3.0

in need it to be

joe 2456 12.00
matt 51235 356.00
tim 12 5.00
jeff 2 3.00

If someone could guide me in right direction i will really appreciate it.

Thanks in advance..
This command:
Code:
sed -r 's/^([^.]+)$/\1\.00/g' sedtest.txt
will print

Code:
joe 2456 12.00
matt 51235 356.00
tim 12 5.00
jeff 2 3.0
if sedtest.txt is
Code:
joe 2456 12
matt 51235 356
tim 12 5.00
jeff 2 3.0
SED, awk and regexp tutorials are available here:
http://www.grymoire.com/Unix/

Last edited by ErV; 01-22-2009 at 12:07 PM.
 
Old 01-22-2009, 06:36 PM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
There's a printf in bash http://www.tldp.org/LDP/abs/html/internal.html#EX47
 
  


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
help to edit Edit my bootloader's config file from kernel prompt jagdishpandey Linux - Kernel 5 07-03-2007 07:59 AM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 06:11 PM
automatically edit txt files quarry_06 Programming 5 04-04-2006 03:19 PM
How to convert a txt file to be a db file in Redhat linux 9? winnie Linux - Newbie 3 06-27-2003 09:33 AM
how to edit a txt file through console safrout Linux - Newbie 5 01-31-2003 08:39 PM


All times are GMT -5. The time now is 07:17 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration