Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Dividing a column by it's first number Post 302534322 by cosmologist on Monday 27th of June 2011 01:46:37 PM
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?
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 10:53 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy