Subtracting values from 2 columns in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Subtracting values from 2 columns in a file
# 1  
Old 10-24-2011
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 chr12 + 8975149 9029377

I want to subtract the value in column 5 from column 4 and send the results to a 6th column and the file to look like this (6th col = values in 5th- values in 4thSmilie

A1BG chr19 + 58863335 58866549 3214
A1BG chr19 - 58858171 58864865 6694
A2LD1 chr13 - 101182417 101186056 3639
A2LD1 chr13 - 101182417 101241046 58629
A2M chr12 - 9220303 9268558 48255
A2ML1 chr12 + 8975149 9029377 54228


Then I want the file to get rid of the duplicates (I already used the sort and uniq commands and I still have some left e.g A1BG, A2LD1)
I want my final file to look like this, with only the genes with greatest length recorded (as below: only the largest values for genes A1BG and ALD1 were recorded:

A1BG chr19 - 58858171 58864865 6694
A2LD1 chr13 - 101182417 101241046 58629
A2M chr12 - 9220303 9268558 48255
A2ML1 chr12 + 8975149 9029377 54228

Please help!
Thanks so much!
# 2  
Old 10-24-2011
AWK

Hi,

Try this one,

Code:
awk '{a=$5-$4;print $0,a;}' Input_File

For unique lines try this one,

Code:
awk 'BEGIN{re=0;}{if(re != $1 ){a=$5-$4;print $0,a;re=$1;}}' Input_File

Cheers,
RangaSmilie

Last edited by rangarasan; 10-24-2011 at 11:22 AM.. Reason: added unique check
This User Gave Thanks to rangarasan For This Post:
# 3  
Old 10-24-2011
The following script only prints the instance of any duplicate with the highest value of the new final field.
Code:
perl -e 'while(<>){
   chomp;
   @fields=split(/ /,$_);
   if (($fields[4] - $fields[3]) > $genes{$fields[0]}->[5]){
      $genes{$fields[0]}=[@fields[0..4],$fields[4] - $fields[3]];
   }
}
for (sort keys %genes){
   print join (" ", @{$genes{$_}}),"\n";
}' tmp.dat

This User Gave Thanks to Skrynesaver For This Post:
# 4  
Old 10-24-2011
To fulfill wolf request, modified ranga's code
Code:
$ nawk '{a=$5-$4;print $0,a}' infile | sort +5nr | nawk '!x[$1]++'
A2LD1 chr13 - 101182417 101241046 58629
A2ML1 chr12 + 8975149 9029377 54228
A2M chr12 - 9220303 9268558 48255
A1BG chr19 - 58858171 58864865 6694

This User Gave Thanks to jayan_jay For This Post:
# 5  
Old 10-24-2011
Sorry to be confused on this one but where do I enter my file name? at the end of:
nawk '{a=$5-$4;print $0,a}' infile | sort +5nr | nawk '!x[$1]++' myfile.txt Am I doing this correctly?
Sorry, I'm really new to Unix.
# 6  
Old 10-24-2011
Code:
nawk '{a=$5-$4;print $0,a}' infile | sort +5nr | nawk '!x[$1]++'

Your file is specified in red.

Then the pipe operator "|" will branch the output of the previous command as input of next command.

so the output of
Code:
nawk '{a=$5-$4;print $0,a}' infile

will be given as input of
Code:
sort +5nr

then output of
Code:
nawk '{a=$5-$4;print $0,a}' infile | sort +5nr

will be given as input of
Code:
nawk '!x[$1]++'


Last edited by ctsgnb; 10-24-2011 at 07:08 PM.. Reason: sry wrong window
 
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. Shell Programming and Scripting

awk - Adding and Subtracting Numbers from 2 Columns

Hi Folks, I have a file with 2 columns TAB delimited and I want to add '1' to the first column and subtract '-1' from the second column. What I have tried so far is; awk -F"\t" '{ $1-=1;$2+=1}1' OFS='\t' file File 0623 0623 0624 0624 0643 0643 1059 1037 1037 1037 1038 1038... (2 Replies)
Discussion started by: pshields1984
2 Replies

3. Shell Programming and Scripting

Subtracting values from variable

Legends, Please help me in , how do i subtract the variable values listed like below. the first value of orig should be subtracted from first value of prev and so on. san> echo $orig 346 316 340 239 410 107 291 139 128 230 167 147 159 159 172 116 110 260 177 0 177 169 168 186 165 366 195... (15 Replies)
Discussion started by: sdosanjh
15 Replies

4. Shell Programming and Scripting

Searching columns and subtracting values in awk

Hi everyone, I had a similar question a couple days ago but my problem has gotten significantly (to me anyway) more complex. I have two files: File 1: 0808 166 166 62 9 0 1000fights 1 1 2 1 0 100places2visit 2 2 2 2 0 10veronica91 167 167 3 1 0 11thgorgeous 346 346 3806 1461 122... (2 Replies)
Discussion started by: collards
2 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. 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

7. Shell Programming and Scripting

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: >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... (3 Replies)
Discussion started by: fabian23
3 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

Subtracting columns against each other

Hi All, I have a file of 100 lines of each having 1000 columns. I need to find the difference of each column against each other. That means, Col1-Col1; Col1-Col2; Col1-Col3;......Col1-Col1000; Col2-Col1; Col2-Col2; Col2-Col3;.... and so on ....up to Col1000-Col1000. Lets say the file is... (6 Replies)
Discussion started by: Fredrick
6 Replies

10. Shell Programming and Scripting

comparing files - adding/subtracting/formating columns

I have two files: file1.txt: FS Total Used Free Used% /u01 10000 8000 2000 80% /u02 10000 8000 2000 80% /u03 10000 8000 2000 80% /u04 10000 8000 2000 80% /u05 10000 8000 2000 80% /u06 10000 8000 2000 80% /u07 10000 8000 2000 80% /u10 10000 5000 5000 50% file2.txt:... (7 Replies)
Discussion started by: oabdalla
7 Replies
Login or Register to Ask a Question