Compute Difference and Edit second, third columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compute Difference and Edit second, third columns
# 1  
Old 04-08-2013
Compute Difference and Edit second, third columns

Hi Friends,

My input file is like this

Code:
chr1 100 200
chr1 300 330
chr1 2000 2000
chr1 5000 5000
chr2 7790 7890
chr2 8000 8000

If the difference of third and second columns is zero, then subtract 500 from second column and add 500 to the third column.

So, my output would be

Code:
chr1 100 200
chr1 300 330
chr1 1500 2500
chr1 4500 5500
chr2 7290 8390
chr2 7500 8500

Thanks

---------- Post updated at 01:54 PM ---------- Previous update was at 01:39 PM ----------

Thanks, I found the answe.

Code:
awk '{(v=$3-$2); if(v==0) print $1"\t"$2-500"\t"$3+500; else print $0}'

But, I don't see the other rows that has a difference that equals to non-zero.
# 2  
Old 04-08-2013
A little more shorter form
Code:
awk '$2==$3{$2-=500;$3+=500}1' input_file

--ahamed
This User Gave Thanks to ahamed101 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculate percentage difference between two columns

I have a input text file in this format: ITEM1 10.9 20.1 ITEM2 11.6 12 ITEM3 14 15.7 ITEM5 20 50.6 ITEM6 25 23.6 I want to print those lines which have more than 5% difference between second and third columns. (8 Replies)
Discussion started by: ctrld
8 Replies

2. Shell Programming and Scripting

To transpose columns + edit in a txt file

Hi, I have a txt file that looks like log2FoldChange Ontology_term 8.50624450251828 GO:0003700,GO:0003707,GO:0005634,GO:0006355,GO:0043401,GO:0003700,GO:0005634,GO:0006355,GO:0008270,GO:0043565 7.03936870356684 GO:0005515,GO:0008080 6.49606183738682 6.49525073909629 GO:0005515... (4 Replies)
Discussion started by: alisrpp
4 Replies

3. Shell Programming and Scripting

[Solved] sum up third and second columns by 0 difference

Hi Friends, I have the following file chr1 1 2 chr1 2 3 chr1 3 4 chr1 4 5 chr1 5 6 chr1 19 20 chr1 20 21 chr1 21 22 I want to compare the third column of record 1 to second column of next record and if the difference is zero, consider its third column and match it to next record... (4 Replies)
Discussion started by: jacobs.smith
4 Replies

4. Shell Programming and Scripting

Difference between two columns and count

hi I am very new to shell scripting. i wanted to achieve this.. Col1 Col2 Col3 Col4 aa 23 bb 32 aa 34 bb 12 aa 45 bb 345 kk 20 ss 50 kk 30 ss 50 tt 10 vv 50 Desired output is Col1 Col2 Col3 Col4 Difference Count aa 23 bb ... (1 Reply)
Discussion started by: empyrean
1 Replies

5. Shell Programming and Scripting

Easy edit problem: interchange columns

hi; my file1.txt: cell137 1 cell337 1 cell355 1 cell355 3 cell360 1 cell360 2 cell360 3 my file2.txt: ... cell137 1 20.64.1.97 cell137 2 20.64.1.97 cell137 3 20.64.1.97 ... cell337 1 20.64.1.113 cell337 2 20.64.1.113 cell337 3 20.64.1.113 (4 Replies)
Discussion started by: gc_sw
4 Replies

6. Shell Programming and Scripting

How to calculate the difference between two adjacent columns?

Dear All, I need to find the difference between two adjacent columns. The file is having 'i' columns and i need to find the difference between two adjacent columns (like $1 difference $2; $2 difference $3; .... and $(i-1) difference $i). I have used the following coding awk '{ for (i=1; i<NF;... (7 Replies)
Discussion started by: Fredrick
7 Replies

7. Shell Programming and Scripting

Extract difference of two columns from different rows

Hello guys, Please help me to solve this problem. I have tried some awk commands but couldn't succeed. I have a tab delimited file where each record is separated by ------ and 4th column of each record is same. <INPUT FILE> ------ peon 53931587 53931821 ... (12 Replies)
Discussion started by: sam_2921
12 Replies

8. Shell Programming and Scripting

edit columns in unix shell script

i have a file which if you will open as xls will yield something like this: Column1 Column2 Column3 ABC CDE EFG GHI IJK KLM MNO OPQ QRS what i want to need is to have this kind of results: Column1 ABC CDE EFG GHI IJK ... (10 Replies)
Discussion started by: kingpeejay
10 Replies

9. Shell Programming and Scripting

Compute difference between 2 arrays perl

Hi, I have 2 arrays. I have to compute symmetric difference! @arr=qw(19205134 18630215 18453487 18416242 18338715 18227590 17698645); @arr1=qw(18227590 18053561 17698645 16966777); #The code which i used is this! @union = @isect = @diff = (); %union = %isect = (); %count =... (3 Replies)
Discussion started by: vanitham
3 Replies

10. UNIX for Dummies Questions & Answers

how to get difference of two columns

hey guys i want to divide the 2nd column with the first column using any command of unix help would be appreciated input set1 10 40 set2 12 60 output 2 20 (10 Replies)
Discussion started by: bogu0001
10 Replies
Login or Register to Ask a Question