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
# 8  
Old 06-25-2011
Try this awk script:
Code:
~/unix.com$ awk 'NR==FNR{if(NR==460){x=$2}next}{$2=$2-x}1' infile infile


Last edited by tukuyomi; 06-25-2011 at 02:37 PM.. Reason: Replaced divide by substract
This User Gave Thanks to tukuyomi For This Post:
# 9  
Old 06-25-2011
Is there a way to skip the columns that have all entries = 0?

Thanks!
# 10  
Old 06-25-2011
Try:
Code:
awk '$2&&!y{x=$2;y=1;next}y{print $2/x}' file

shorter..
Code:
awk 'y{$0=$2/x}y;$2&&!y{x=$2;y=1}' file


Last edited by bartus11; 06-25-2011 at 04:18 PM..
# 11  
Old 06-25-2011
Quote:
Originally Posted by bartus11
Try:
Code:
awk '$2&&!y{x=$2;y=1;next}y{print $2/x}' file

Thanks again for your help but ... it turns out that the columns that start with zero the are all zero (the whole column = 0 ).

So what I am trying to do now is to find a way to have "awk" look at column by column. If it has 0s then it creates a column that says "skip". If the values are non-zero then it divided that column by the first value. So for the example for the file:

Code:
671            9770.               0.               0.               0.               0.        11865563.        11944498.        11973309.        11995017.        12024223.        12047903.        12048693.
  672            9790.               0.               0.               0.               0.        11778218.        11856831.        11885525.        11907537.        11935052.        11960994.        11964138.
  673            9810.               0.               0.               0.               0.        11687964.        11765082.        11793659.        11815972.        11845333.        11869212.        11875084.
  674            9830.               0.               0.               0.               0.        11595616.        11666963.        11692695.        11713748.        11740650.        11756244.        11771060.
  675            9850.               0.               0.               0.               0.        11516347.        11589346.        11614196.        11632058.        11654579.        11661568.        11672441.
  676            9870.               0.               0.               0.               0.        11441105.        11509168.        11533144.        11551707.        11574523.        11576457.        11581097.
  677            9890.               0.               0.               0.               0.        11332098.        11392952.        11410284.        11422224.        11436475.        11435705.        11435705.
  678            9910.               0.               0.               0.               0.        11289472.        11356219.        11379235.        11394963.        11397649.        11401868.        11391511.
  679            9930.               0.               0.               0.               0.        11205454.        11267730.        11286069.        11299059.        11293328.        11289889.        11268112.
  680            9950.               0.               0.               0.               0.        11130010.        11194699.        11214867.        11211443.        11211062.        11192036.        11145612.

it would list the first column, the second, and then for the third column it would print "skip" the same for columns 4,5 and 6 then it would divide column 7 by 11865563. and column 8 by 11944498. ... etc

Is that possible?
# 12  
Old 06-25-2011
Post desired output for that sample.
# 13  
Old 06-25-2011
Quote:
Originally Posted by bartus11
Post desired output for that sample.
The output would be something like:

Code:
671  9770.  skip.  skip.  skip.  skip.  1  1  1  1  1  1  1
672  9790.  skip.  skip.  skip.  skip.  0.992639  0.99266  0.992668  0.992707  0.992584  0.992786  0.992982
673  9810.  skip.  skip.  skip.  skip.  0.985032  0.984979  0.984996  0.985073  0.985123  0.985168  0.985591
674  9830.  skip.  skip.  skip.  skip.  0.97725  0.976765  0.976563  0.976551  0.976417  0.975792  0.976957
675  9850.  skip.  skip.  skip.  skip.  0.970569  0.970266  0.970007  0.969741  0.969258  0.967933  0.968772
676  9870.  skip.  skip.  skip.  skip.  0.964228  0.963554  0.963238  0.963042  0.9626  0.960869  0.961191
677  9890.  skip.  skip.  skip.  skip.  0.955041  0.953824  0.952977  0.952247  0.95112  0.949186  0.949124
678  9910.  skip.  skip.  skip.  skip.  0.951448  0.950749  0.950383  0.949975  0.947891  0.946378  0.945456
679  9930.  skip.  skip.  skip.  skip.  0.944368  0.943341  0.942602  0.941979  0.939215  0.937083  0.935214
680  9950.  skip.  skip.  skip.  skip.  0.938009  0.937226  0.936656  0.934675  0.932373  0.928961  0.925047

# 14  
Old 06-25-2011
Took some time, but here it is:
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++){
  if (grep {$_ != 0} @{$c[$i]}){
    for $j (@{$c[$i]}){
      $d[$i]=$j if ($j!=0 && !$d[$i]);
    }
    @{$c_out[$i]}=map {($_/$d[$i])} @{$c[$i]};
  } else {
    @{$c_out[$i]}=map {("skip.")} @{$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";
}

Run this script like this: ./script.pl your_file
This User Gave Thanks to bartus11 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

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