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
# 15  
Old 06-25-2011
Thanks you SOO MUCHH!
# 16  
Old 06-25-2011
I come with an awk solution a bit late Smilie
Code:
awk '
BEGIN{getline;for(i=1;i<=NF;i++)A[i]=$i}
NR==FNR{next}
{
	for(i=1;i<=NF;i++){
		if($i==0){$i="skip";j=i}
		else {
			if(i>j&&j>0){$i=$i/A[i]}
		}
	}
}1' infile infile


Last edited by tukuyomi; 06-25-2011 at 06:26 PM.. Reason: Added [code] tags
This User Gave Thanks to tukuyomi For This Post:
# 17  
Old 06-27-2011
I am trying to edit any one of the suggested codes so that I can do the following:

Leave columns 1 and 2 as they are.
For columns 3 -13 if the values are 1 then make it 0 (only if the whole column = 1, if it is 1 in columns that have other numbers treat it as any other number), if the value is other that 1 then subtract it by column 1.

I can't get it right Smilie
Any help would be highly appreciated...
Thanks
# 18  
Old 06-27-2011
Try:
Code:
#!/usr/bin/perl
open I,"$ARGV[0]";
chomp(@x=<I>);
for ($i=0;$i<=$#x;$i++){
  @y=split / +/,$x[$i];
  for ($j=0;$j<=$#y;$j++){
    push @{$c[$j]},$y[$j];
  }
}
for ($i=0;$i<=$#y;$i++){
  for $j (@{$c[$i]}){
    $d[$i]=$j if ($j!=0 && !$d[$i]);
  }
  @one=grep {$_ == 1} @{$c[$i]};
  if ($#one==$#{$c[$i]}){
    @{$c_out[$i]}=map {("0")} @{$c[$i]};
  } else {
    @{$c_out[$i]}=map {($_-$d[$i])} @{$c[$i]};
  }
}
for ($i=0;$i<=$#x;$i++){
  for ($j=0;$j<=$#y;$j++){
    if ($j<2){
      print $c[$j][$i],"  ";
    } else {
      print $c_out[$j][$i],"  ";
    }
  }
  print "\n";
}

# 19  
Old 06-27-2011
Quote:
Originally Posted by bartus11
Try:
Code:
#!/usr/bin/perl
open I,"$ARGV[0]";
chomp(@x=<I>);
for ($i=0;$i<=$#x;$i++){
  @y=split / +/,$x[$i];
  for ($j=0;$j<=$#y;$j++){
    push @{$c[$j]},$y[$j];
  }
}
for ($i=0;$i<=$#y;$i++){
  for $j (@{$c[$i]}){
    $d[$i]=$j if ($j!=0 && !$d[$i]);
  }
  @one=grep {$_ == 1} @{$c[$i]};
  if ($#one==$#{$c[$i]}){
    @{$c_out[$i]}=map {("0")} @{$c[$i]};
  } else {
    @{$c_out[$i]}=map {($_-$d[$i])} @{$c[$i]};
  }
}
for ($i=0;$i<=$#x;$i++){
  for ($j=0;$j<=$#y;$j++){
    if ($j<2){
      print $c[$j][$i],"  ";
    } else {
      print $c_out[$j][$i],"  ";
    }
  }
  print "\n";
}

Thank you for your reply. I tried it but it does the same problem: It replaces "all" 1s with 0s.

Here is what I mean:

Input:

Code:
  1 0.835034   4550.  1  1  1  1  1  1  1  1  1  1  1
  2 0.837456   4570.  1  1  1  1  1  0.986992955332099  0.98811543466274  0.988886099536191  0.989214304977886  0.989264543507699  0.989318834482632
  3 0.839372   4590.  1  1  1  1  1  0.972724966680894  0.973164551250903  0.973601709054383  0.973775211679668  0.973688370612099  0.973504640892287
  4 0.841537   4610.  1  1  1  1  1  0.95430147882864  0.956223291120844  0.958043454853154  0.9592384234103  0.959749749532396  0.960122266730889
  5 0.843855   4630.  1  1  1  1  1  0.940596580817871  0.940360268167827  0.94111598259216  0.941694996631145  0.94194732194985  0.942302439440776
  6 0.846596   4650.  1  1  1  1  1  0.923615782059084  0.925875436355658  0.928001690635942  0.929592500523555  0.930547367852846  0.931170903119468
  7 0.849154   4670.  1  1  1  1  1  0.917660188314667  0.91763873045508  0.918062066325892  0.918317580831534  0.918212206178722  0.918110759512965
  8 0.85076   4690.  1  1  1  1  1  0.909903702024204  0.908333170296792  0.907504539512332  0.90676771242569  0.905998803112881  0.905347746824947
  9 0.852449   4710.  1  1  1  1  1  0.879271271933478  0.876744973450331  0.87649822003663  0.876651777908988  0.876782216262364  0.877398641164623
 10 0.853877   4730.  1  1  1  1  1  0.875053229272454  0.873863419031994  0.874550610648734  0.875373099539625  0.876003975824422  0.876464568865476

output

Code:
  1 0.835034  4550.  0  0  0  0  0  0  0  0  0  0  0
  2 0.837456  4570.  0  0  0  0  0  -0.013007044667901  -0.01188456533726  -0.011113900463809  -0.010785695022114  -0.010735456492301  -0.010681165517368
  3 0.839372  4590.  0  0  0  0  0  -0.027275033319106  -0.026835448749097  -0.026398290945617  -0.026224788320332  -0.026311629387901  -0.0264953591077129      
  4 0.841537  4610.  0  0  0  0  0  -0.04569852117136  -0.043776708879156  -0.041956545146846  -0.0407615765897  -0.040250250467604  -0.039877733269111
  5 0.843855  4630.  0  0  0  0  0  -0.059403419182129  -0.059639731832173  -0.05888401740784  -0.058305003368855  -0.05805267805015  -0.057697560559224
  6 0.846596  4650.  0  0  0  0  0  -0.076384217940916  -0.0741245636443419  -0.071998309364058  -0.070407499476445  -0.069452632147154  -0.068829096880532      
  7 0.849154  4670.  0  0  0  0  0  -0.082339811685333  -0.08236126954492  -0.081937933674108  -0.081682419168466  -0.081787793821278  -0.0818892404870351
  8 0.85076  4690.  0  0  0  0  0  -0.090096297975796  -0.0916668297032081  -0.092495460487668  -0.09323228757431  -0.0940011968871191  -0.094652253175053
  9 0.852449  4710.  0  0  0  0  0  -0.120728728066522  -0.123255026549669  -0.12350177996337  -0.123348222091012  -0.123217783737636  -0.122601358835377
 10 0.853877  4730.  0  0  0  0  0  -0.124946770727546  -0.126136580968006  -0.125449389351266  -0.124626900460375  -0.123996024175578  -0.123535431134524

You notice that for example the first raw is now replaced by 0s. But what I would like to have is ONLY the columns where all entries are 1 to become all 0s. So for example for that first line in this case only the first 5 1s will become 0s, the rest will remain 1s.

Does it make sense?
# 20  
Old 06-27-2011
Zeros in columns 9-14 are result of subtraction... I don't know if it is what you wanted, but my code is taking first number in each column that is other than zero, and then subtracts its value from all the other numbers in that column. If this is not how it should work, then please be more clear on your requirements and post desired output for that sample data.
# 21  
Old 06-27-2011
Quote:
Originally Posted by bartus11
Zeros in columns 9-14 are result of subtraction... I don't know if it is what you wanted, but my code is taking first number in each column that is other than zero, and then subtracts its value from all the other numbers in that column. If this is not how it should work, then please be more clear on your requirements and post desired output for that sample data.

Sorry for not being clear.
So here is an example:
Input file:

Code:
0.835034   4550.  1  1  1  1  1  1  1  1  1  1  1
0.837456   4570.  1  1  1  1  1  0.986992955332099  0.98811543466274  0.988886099536191  0.989214304977886  0.989264543507699  0.989318834482632
0.839372   4590.  1  1  1  1  1  0.972724966680894  0.973164551250903  0.973601709054383  0.973775211679668  0.973688370612099  0.973504640892287
0.841537   4610.  1  1  1  1  1  0.95430147882864  0.956223291120844  0.958043454853154  0.9592384234103  0.959749749532396  0.960122266730889
0.843855   4630.  1  1  1  1  1  0.940596580817871  0.940360268167827  0.94111598259216  0.941694996631145  0.94194732194985  0.942302439440776
0.846596   4650.  1  1  1  1  1  0.923615782059084  0.925875436355658  0.928001690635942  0.929592500523555  0.930547367852846  0.931170903119468
0.849154   4670.  1  1  1  1  1  0.917660188314667  0.91763873045508  0.918062066325892  0.918317580831534  0.918212206178722  0.918110759512965
0.85076   4690.  1  1  1  1  1  0.909903702024204  0.908333170296792  0.907504539512332  0.90676771242569  0.905998803112881  0.905347746824947
0.852449   4710.  1  1  1  1  1  0.879271271933478  0.876744973450331  0.87649822003663  0.876651777908988  0.876782216262364  0.877398641164623
0.853877   4730.  1  1  1  1  1  0.875053229272454  0.873863419031994  0.874550610648734  0.875373099539625  0.876003975824422  0.876464568865476
0.854173   4750.  1  1  1  1  1  0.863588025530899  0.862040940671601  0.86200807455608  0.862265862563867  0.862501149096866  0.861994272811708
0.854461   4770.  1  1  1  1  1  0.863380744335004  0.860456824396868  0.859060319690651  0.858189664456392  0.857016126724736  0.854518036171929
0.856634   4790.  1  1  1  1  1  0.848713255592577  0.845859896644149  0.844988811701903  0.844137247036313  0.842064879886148  0.839732419584626
0.859741   4810.  1  1  1  1  1  0.832330509966605  0.829342015794448  0.828000564617712  0.82648056444271  0.824234295625436  0.820301947609844
0.863014   4830.  1  1  1  1  1  0.823941656428126  0.818458278549728  0.814714353806791  0.809954468128954  0.802266201657013  0.790394917882526
0.865703   4850.  1  1  1  1  1  0.80023588959194  0.776351479766234  0.752155596014016  0.725500011780627  0.695976669427205  0.66401494596973
0.868021   4870.  1  1  1  1  1  0.727574054897996  0.691750487412714  0.661899353474515  0.632966017087269  0.603742494342791  0.574485254217654
0.869482   4890.  1  1  1  1  1  0.793287430889227  0.786603666685166  0.781380444059364  0.774219022790407  0.763516626036496  0.747953668306781
0.871363   4910.  1  1  1  1  1  0.782697486461136  0.777430016398149  0.77424214197622  0.771924429653325  0.768854066828836  0.764104064921683

Needed output:

Code:
0.835034  4550.  0  0  0  0  0  0.835034  0.835034  0.835034 0.835034  0.835034  0.835034
0.837456  4570.  0  0  0  0  0  0.826563  0.827503  0.828149 0.828423  0.828466  0.828511
0.839372  4590.  0  0  0  0  0  0.816478  0.816847  0.817214 0.81736  0.817287  0.817133
0.841537  4610.  0  0  0  0  0  0.80308  0.804697  0.806229 0.807235  0.807665  0.807978
0.843855  4630.  0  0  0  0  0  0.793727  0.793528  0.794165 0.794654  0.794867  0.795167
0.846596  4650.  0  0  0  0  0  0.781929  0.783842  0.785643 0.786989  0.787798  0.788326
0.849154  4670.  0  0  0  0  0  0.779235  0.779217  0.779576 0.779793  0.779704  0.779617
0.85076  4690.  0  0  0  0  0  0.77411  0.772774  0.772069 0.771442  0.770788  0.770234
0.852449  4710.  0  0  0  0  0  0.749534  0.74738  0.74717 0.747301  0.747412  0.747938
0.853877  4730.  0  0  0  0  0  0.747188  0.746172  0.746759 0.747461  0.748  0.748393
0.854173  4750.  0  0  0  0  0  0.737654  0.736332  0.736304 0.736524  0.736725  0.736292
0.854461  4770.  0  0  0  0  0  0.737725  0.735227  0.734034 0.73329  0.732287  0.730152
0.856634  4790.  0  0  0  0  0  0.727037  0.724592  0.723846 0.723117  0.721341  0.719343
0.859741  4810.  0  0  0  0  0  0.715589  0.713019  0.711866 0.710559  0.708628  0.705247
0.863014  4830.  0  0  0  0  0  0.711073  0.706341  0.70311 0.699002  0.692367  0.682122
0.865703  4850.  0  0  0  0  0  0.692767  0.67209  0.651143 0.628068  0.602509  0.57484
0.868021  4870.  0  0  0  0  0  0.63155  0.600454  0.574543 0.549428  0.524061  0.498665
0.869482  4890.  0  0  0  0  0  0.689749  0.683938  0.679396 0.67317  0.663864  0.650332
0.871363  4910.  0  0  0  0  0  0.682014  0.677424  0.674646 0.672626  0.669951  0.665812

So here (starting with column 3) when a column is all = 1 it all becomes =0 , the other columns are multiplied by the first column.
 
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