Arithmetic in Unix shell Script

user1879573

I would like to calculate the average of columns 2 and 3 on each line, add 1 to this value and print the whole line. Some of the average values will be floating point numbers.

The input file looks like this:

chr20 2330559 2330737
chr20 2332853 2333041
chr20 2537555 2537711

The output file:

chr20 2330648 2330649
chr20 2332947 2332948
chr20 2537633 2537634

I've tried various combinations of awk without success. Any suggestions would be great! Thanks Harriet

user000001

Use awk for this:

awk '{$2=($2+$3)/2; $3=$2+1}1' file

You can also use the int() function to ensure that the result is an integer:

awk '{$2=int(($2+$3)/2); $3=$2+1}1' file

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Arithmetic syntax error with shell script

From Dev

Unix Shell Script

From Dev

Shell Script - Unix

From Dev

Shell script, linux, unix, shell

From Dev

Unix Shell Script: While loop

From Dev

Unix shell script - purpose of colon

From Dev

Beginner Unix shell script issues

From Dev

unix shell script to check for EOF

From Dev

Beginner Unix shell script issues

From Dev

missing `]' error in unix shell script

From Dev

Nested expression in a UNIX shell script

From Dev

Run Perl Script From Unix Shell Script

From Dev

How to invoke octave script in unix shell

From Dev

unix shell script to get nth business day

From Dev

Unix shell script: exit with returning value

From Dev

To run unix shell script through JAVA applications

From Dev

Unix bash shell script - Iterating an array in a 'for' loop

From Dev

Previous day date in unix shell script

From Dev

unix shell script for checking three variables are same

From Dev

unix shell script variable declaration - command not found

From Dev

catch spark return code in Unix shell script

From Dev

How use echo with grep in a Unix shell script?

From Dev

to establish database connection using UNIX shell script

From Dev

unix shell script variable declaration - command not found

From Dev

Use variables with dot in unix shell script

From Dev

Trim a specific field and replace in Unix Shell Script

From Dev

Two-words argument in shell script - unix

From Dev

How to Pivot the data by awk in unix shell script?

From Dev

How to replace string in unix shell script