Math operations with file columns values.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Math operations with file columns values.
# 1  
Old 08-29-2011
Math operations with file columns values.

Hello everybody,

I have some large files containing statistical data. The data is stored in the following generic format:
Code:
>cat my_file
1, 2, 3
1, 2, 3
1, 2, 3

>

The values of columns no.2 and 3 are expressed in bytes. I would like to transform them in Megabytes, by dividing them with 1048576. How could I do that automatically?

Thank you,
fabian

Last edited by radoulov; 08-29-2011 at 05:07 AM.. Reason: Code tags.
# 2  
Old 08-29-2011
Code:
 
$ nawk -F, '{print $1","$2/1048576"MB,"$3/1048576"MB"}' test                                                                                      
1,1MB,1MB
2,2MB,2MB
3,2MB,2MB

$ cat test
1,1048576,1048576
2,2097152,2097152
3,2097152,2097152

---------- Post updated at 01:25 PM ---------- Previous update was at 01:24 PM ----------

if you want to test handle some floating points in output, then try printf
# 3  
Old 08-29-2011
Quote:
Originally Posted by itkamaraj
Code:
 
$ nawk -F, '{print $1","$2/1048576"MB,"$3/1048576"MB"}' test                                                                                      
1,1MB,1MB
2,2MB,2MB
3,2MB,2MB

$ cat test
1,1048576,1048576
2,2097152,2097152
3,2097152,2097152

---------- Post updated at 01:25 PM ---------- Previous update was at 01:24 PM ----------

if you want to test handle some floating points in output, then try printf
Thank you very much for your quickly reply.
Right, but I forgot to tell that I would like to save them in a new file.
What is the option for that?

fabian
# 4  
Old 08-29-2011
Code:
 
nawk -F, '{print $1","$2/1048576"MB,"$3/1048576"MB"}' input > output

This User Gave Thanks to itkamaraj For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Add values to file in 2 new columns

Columns 4 and 5 are X and Y coordinates, column 6 is the elevation I would like to add 2 new columns at the end of the file with values the distance between first(X)(Y) and last location (X)(Y), based in 2 rows the difference in elevation = ($6-prev6) How to calculate the requested values... (6 Replies)
Discussion started by: jiam912
6 Replies

2. UNIX for Beginners Questions & Answers

Awk: compare values in two columns of the same file

I'm trying to learn awk, but I've hit a roadblock with this problem. I have a hierarchy stored in a file with 3 columns: id name parentID 4 D 2 2 B 1 3 C 1 1 A 5 I need to check if there are any values in column 3 that are not represented anywhere in column 1. I've tried this: awk '{arr;}... (7 Replies)
Discussion started by: kaktus
7 Replies

3. Shell Programming and Scripting

Identifying columns and their values based on schema file

I have 3 files, data file,schema file and a threshold file. Data file contains data in which columns are distributed according to schema file. This data file doesn't contain any headers. Three continuous columns in the data file represent single variable in schema file. first column represent... (1 Reply)
Discussion started by: bharathbangalor
1 Replies

4. Shell Programming and Scripting

Help needed with multiplying two values of two columns in a file

Hi, I am trying to multiply column#1 with column#2 using a shell script. How can I make a for-loop script using 1st column as "i" and the second column as "j" from the following file? Please feel free to share any alternative ways to multiplying column#1 with column#2. .06 5.0000 .49 ... (6 Replies)
Discussion started by: momin
6 Replies

5. UNIX for Dummies Questions & Answers

Removing columns from a text file that do not have any values in second and third columns

I have a text file that has three columns. But at the end of the text file, there are trailing lines that have missing second and third columns: 4 0.04972604 KLHL28 4 0.0497332 CSTB 4 0.04979822 AIF1 4 0.04983331 DECR2 4 0.04990344 KATNB1 4 4 4 4 How can I remove the trailing... (3 Replies)
Discussion started by: evelibertine
3 Replies

6. UNIX for Dummies Questions & Answers

Subtracting values from 2 columns in a file

Hello, I have a file with 5 columns that looks like this: A1BG chr19 + 58863335 58866549 A1BG chr19 - 58858171 58864865 A2LD1 chr13 - 101182417 101186056 A2LD1 chr13 - 101182417 101241046 A2M chr12 - 9220303 9268558 A2ML1 ... (5 Replies)
Discussion started by: wolf_blue
5 Replies

7. Shell Programming and Scripting

Copy values from columns matching in those in second file.

Hi All, I have two sets of files. Set 1: 100 text files with extension .txt with names like 1.txt, 2.txt, 3.txt until 100.txt Set 2: One big file with extension .dat The text files have some records in columns like this: 0.7316431 82628 0.7248189 82577 0.7248182 81369 0.7222999... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

8. UNIX for Dummies Questions & Answers

combine the values from the first two columns within a file

Hello everybody, I have a text file containing 10,000 rows and 5000 columns. The values are separated by a tab. Ex. file_ex.ped 1 mike 0 0 2 1 A A G G C T A G 1 jack 0 0 2 2 T A G T C A A C 1 Mary 0 0 1 2 A T G C A T G C ... I would like a out put file 1 mike 0 0 2 1 AA GG CT AG 1... (7 Replies)
Discussion started by: Unilearn
7 Replies

9. Shell Programming and Scripting

sending field values in columns to output file

Hi, I am trying to send output from a shell scrip to a txt file in a colum format. I have all I can and I dont seem to understand why the results from one one particular file keeps going to the next line. How can I force all to be in one line Please see code and output below The is... (0 Replies)
Discussion started by: asemota
0 Replies

10. Shell Programming and Scripting

Operations on columns of 2 files

Hi I have 2 file with many lines and colums and i want to do some operation for each value in the 2 files : Matrix1 : a11 a12 a13 a14 ... a21 a22 a23 a42 ... a31 a32 a33 a32 ... ... Matrix2 : b11 b12 b13 b14 ... b21 b22 b23 b42 ... b31 b32 b33 b32 ... ... I want to have the... (8 Replies)
Discussion started by: rauchy
8 Replies
Login or Register to Ask a Question