LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-29-2014, 05:36 AM   #1
santosh0782
Member
 
Registered: Nov 2013
Posts: 132

Rep: Reputation: Disabled
How to round values in a column in a file


Hi,

I have a file test.txt.

cat test.txt
2014-07-25 00:00|739899.65|
2014-07-25 00:15|571028.09|
2014-07-25 00:30|64298.41|
2014-07-25 00:45|38963.27|
2014-07-25 01:00|166980.95|
2014-07-25 01:15|54133.68|
2014-07-25 01:30|610695.42|
2014-07-25 01:45|60568.26|
2014-07-25 02:00|122777.71|


how to round the numbers in 2nd column?
 
Old 07-29-2014, 06:04 AM   #2
eklavya
Member
 
Registered: Mar 2013
Posts: 619

Rep: Reputation: 136Reputation: 136
Run the command
Code:
awk 'BEGIN{OFS=FS="|"}{$2=sprintf("%3.0f",$2)}1' test.txt
 
1 members found this post helpful.
Old 07-29-2014, 06:25 AM   #3
santosh0782
Member
 
Registered: Nov 2013
Posts: 132

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by eklavya View Post
Run the command
Code:
awk 'BEGIN{OFS=FS="|"}{$2=sprintf("%3.0f",$2)}1' test.txt
Thanks a lot :-), its working fine :-)
 
Old 07-29-2014, 07:45 AM   #4
santosh0782
Member
 
Registered: Nov 2013
Posts: 132

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by eklavya View Post
Run the command
Code:
awk 'BEGIN{OFS=FS="|"}{$2=sprintf("%3.0f",$2)}1' test.txt
Hello Sir,

could you please explain the parameters in above command, so that i'll keep in mind for future refrence.

$2 is the second column, actually i didn't get this part {$2=sprintf("%3.0f",$2)}1
 
Old 07-29-2014, 08:56 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,129

Rep: Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271
{ } is a block

sprintf("%3.0f",$2)
means you format the value of $2 as float, 3 digits and 0 decimals => that means rounded).
$2 = sprintf... means $2 is set to rounded $2.

1 is a tricky line. It is a condition and always true, therefore the block belongs to this condition will be executed always. There is no block defined, it means the default will be used which simply prints the line. (it should have been written as 1 { print }).
for detailed explanation please read the man page of awk.... or ask
 
Old 07-29-2014, 08:58 AM   #6
eklavya
Member
 
Registered: Mar 2013
Posts: 619

Rep: Reputation: 136Reputation: 136
OFS is output field separator and FS is input field separator. Both are the Awk Built-in Variables. They define that the columns are separated with pipe (|).
$2 is second column. We need to modify this.
"sprintf()" is a string function. It acts in exactly the same way as "printf()", except that "sprintf()" assigns its output to a variable, not standard output.

You need to understand/learn awk to know how it works. Here is very small and good tutorial about awk which tells us about 8 in-built variables of the language. Take a look.
http://www.thegeekstuff.com/2010/01/...-filename-fnr/
 
  


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
Replace Tab Separated Values with Commas Except Last Column darkangel29 Programming 2 07-15-2013 06:20 AM
Rename duplicate values in a column jv61 Linux - Newbie 4 07-12-2012 09:12 AM
How to display 2 different column field values as one column value in mysql VijayaRaghavanLakshman Linux - General 2 04-16-2012 10:56 AM
How to read CSV data and compare the column values and then write them in new file VijayaRaghavanLakshman Linux - Newbie 9 01-26-2012 10:02 PM
[SOLVED] Delete rows based on values in a column using sed captainentropy Linux - Newbie 6 01-19-2011 09:59 AM


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