Dividing a column by it's first number


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Dividing a column by it's first number
# 1  
Old 06-25-2011
Dividing a column by it's first number

Hi!

Is there an easy way (maybe using awk??) to divide the values of one column of the file by it's first entry..

If I have a column:

3
4
5
6
7

I would like to divide it by 3.

I want to do this for more than 100 files, so it wouldn't be practical to open file by file and manually write the first number in the column for the calculation Smilie
# 2  
Old 06-25-2011
Is it the only column in those files? If not, then post sample input and desired output, please.
# 3  
Old 06-25-2011
try this:
Code:
awk 'BEGIN{first_line=0;divide_by=1;}{if(first_line==0){first_line=1; divide_by=$1;}print $1/divide_by;}END{}' your_file

it doesn't care about the number of column the file has, it considers only the first column

Last edited by Scott; 06-25-2011 at 11:23 AM.. Reason: Code tags
This User Gave Thanks to ggmas For This Post:
# 4  
Old 06-25-2011
The file is really big, but this is a sample of the first few lines:
Code:
5200.  1.212681E-13
5205.40000000001  1.222450E-13
5210.8  1.227840E-13
5216.20000000001  1.238888E-13
5221.60000000001  1.254731E-13
5227.00000000001  1.260795E-13
5232.40000000001  1.232285E-13
5237.80000000001  1.223140E-13
5243.20000000001  1.253938E-13
5248.60000000001  1.263059E-13
5254.00000000001  1.242172E-13
5259.40000000001  1.224269E-13
5264.80000000001  1.207516E-13
5270.20000000001  1.192588E-13
5275.60000000001  1.194812E-13
5281.00000000001  1.213403E-13
5286.40000000001  1.237752E-13
5291.80000000001  1.245416E-13
5297.20000000001  1.245865E-13
5302.6  1.251646E-13
5308.00000000001  1.254696E-13
5313.40000000001  1.249386E-13
5318.80000000001  1.252902E-13
5324.20000000001  1.243008E-13
5329.6  1.224932E-13
5335.00000000001  1.228107E-13
5340.40000000001  1.217807E-13
5345.80000000001  1.205050E-13
5351.20000000001  1.213104E-13
5356.60000000001  1.228911E-13
5362.  1.238935E-13
5367.40000000001  1.224568E-13
5372.8  1.207849E-13
5378.20000000001  1.212063E-13
5383.60000000001  1.219594E-13
5389.00000000001  1.222528E-13

I would like to divide the second column by its first number ( 1.212681E-13 in this case).
# 5  
Old 06-25-2011
Code:
awk 'BEGIN{first_line=0;divide_by=1;}{if(first_line==0){first_line=1; divide_by=$2;}print $2/divide_by;}END{}' your_file

The column is indicated by the value after $, in this case $2 means that awk takes the column 2
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 06-25-2011 at 08:41 AM.. Reason: code tags, please!
# 6  
Old 06-25-2011
another awk solution:
Code:
~/unix.com$ awk 'BEGIN{getline;x=$2;print}{$2=$2/x}1' input_file > output_file

# 7  
Old 06-25-2011
Is there a way to get it divide the column by the first non-zero value in that column? Smilie ... some columns have the first 50+ entries = 0 ... I wonder if the first non zero value in the column can be found and divide the whole column by it?

Thanks!

Last edited by cosmologist; 06-25-2011 at 03:41 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Dividing a column by it's last number

Hi! I have a file as below: 0.000145 5.82056e-08 0.000146 6.82088e-08 0.000150 7.42115e-08 0.000156 8.52142e-08 0.000176 8.82169e-08 0.000178 9.82197e-08 I'd like to divide the values of the second column by its last entry (here 9.82197e-08). I would be thankful if someone could... (6 Replies)
Discussion started by: Setareh12
6 Replies

2. Shell Programming and Scripting

Adding number in one column

Hello I have something like this a 1 b 1 c 1 d 1 e 1 This is inside 1.dat and this is what I am trying to get a 2 b 2 c 2 d 2 e 2 (4 Replies)
Discussion started by: jeo_fb
4 Replies

3. Shell Programming and Scripting

Split column data if the table has n number of column's with some record

Split column data if the table has n number of column's with some record then how to split n number of colmn's line by line with records Table --------- Col1 col2 col3 col4 ....................col20 1 2 3 4 .................... 20 a b c d .................... v ... (11 Replies)
Discussion started by: Priti2277
11 Replies

4. Shell Programming and Scripting

Split column data if the table has n number of column's

please write a shell script Table -------------------------- 1 2 3 a b c 3 4 5 c d e 7 8 9 f g h Output should be like this --------------- 1 2 3 3 4 5 7 8 9 a b c c d e f g h (1 Reply)
Discussion started by: Priti2277
1 Replies

5. Shell Programming and Scripting

Dividing a column and submitting it as stdin

I currently have two programs written in C named "remove" and "calculate. When I call ./remove it removes some data from stdin. When I call ./calculate which takes in an argument and also data from stdin and calculates and returns a value. Currently writing a script that calls both of these... (1 Reply)
Discussion started by: kadanakanz
1 Replies

6. Shell Programming and Scripting

Help with compare two column and print out column with smallest number

Input file : 5 20 500 2 20 41 41 0 23 1 Desired output : 5 2 20 0 1 By comparing column 1 and 2 in each line, I hope can print out the column with smallest number. I did try the following code, but it don't look good :( (2 Replies)
Discussion started by: perl_beginner
2 Replies

7. Shell Programming and Scripting

How to get the column number in awk?

Hi Guys, I have a question on how i can get the column number in a file and used it in awk. i have a file which it has these records inside it. ... (7 Replies)
Discussion started by: reignangel2003
7 Replies

8. Shell Programming and Scripting

Dividing by zero

Does anyone know how to include as a script maybe an "echo" warning that explains that if a user uses the second number "zero" when dividing, that the result will BE "zero." I need, example: 5/0 (second number) = 0, in script form. current script: echo "Enter a number" read num1 echo... (4 Replies)
Discussion started by: jefferj54
4 Replies

9. UNIX for Dummies Questions & Answers

Grep by column number

I have a data file that is arranged like this: Marketing Ranjit Singh Eagles Dean Johnson FULL Marketing Ken Whillans Eagles Karen Thompson FULL Sales Peter RobertsonGolden TigersRich Gardener PART President Sandeep Jain Wimps Ken Whillans CONT... (7 Replies)
Discussion started by: hitman247m
7 Replies

10. Shell Programming and Scripting

returning a column number

Hi all, i was doing a small program where if i was to be given the first 3 letters of any month i.e. in the form of Jan or Apr then it would return the column number where it finds a match. To do this i created a 12 element array of months with first 3 letters and if i echo'ed the contents of... (2 Replies)
Discussion started by: scriptingmani
2 Replies
Login or Register to Ask a Question