C shell--take the minimum of a column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting C shell--take the minimum of a column
# 1  
Old 02-23-2011
C shell--take the minimum of a column

I have a data file with two columns,
for the second column I want to find the minimum,
and subtract this minimum from each value in the second column,
how to realize this using C shell

For example, I have
Code:
1  -2.4
2  -4.8
3   7.9

I wanna output
Code:
1    2.4
2    0
3  12.7

Thanks!
# 2  
Old 02-23-2011
perl

Code:
my $min=0;
while(<DATA>){
  chomp;
  my @tmp = split;
  $hash{$.}->{val}=[@tmp];
  $min=($tmp[1]<=$min)?$tmp[1]:$min;
}
foreach my $key (sort {$a<=>$b} keys %hash){
  print $hash{$key}->{val}->[0]," ",$hash{$key}->{val}->[1]-$min,"\n";
}
__DATA__
1  -2.4
2  -4.8
3   7.9

# 3  
Old 02-23-2011
Thanks a lot, summer cherry!
Anyone can help on a C shell version?
Appreciate your time!
# 4  
Old 02-23-2011
data
Code:
1  -2.4
2  -4.8
3   7.9

Code:
set asd = `awk '$2< min {min=$2;} END{ print min}' data`
awk '{$2=$2 - '"$asd"'} {print $0}' data


Last edited by Franklin52; 02-24-2011 at 03:24 AM.. Reason: Please use code tags, thank you
# 5  
Old 02-23-2011
It works.
Thanks a lot!


Quote:
Originally Posted by Abid
data
1 -2.4
2 -4.8
3 7.9


set asd = `awk '$2< min {min=$2;} END{ print min}' data`
awk '{$2=$2 - '"$asd"'} {print $0}' data
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to insert data into black column( Secound Column ) in excel (.XLSX) file using shell script?

Source Code of the original script is down below please run the script and try to solve this problem this is my data and I want it column wise 2019-03-20 13:00:00:000 2019-03-20 15:00:00:000 1 Operating System LAB 0 1 1 1 1 1 1 1 1 1 0 1 (5 Replies)
Discussion started by: Shubham1182
5 Replies

2. Shell Programming and Scripting

Need awk or Shell script to compare Column-1 of two different CSV files and print if column-1 matche

Example: I have files in below format file 1: zxc,133,joe@example.com cst,222,xyz@example1.com File 2 Contains: hxd hcd jws zxc cst File 1 has 50000 lines and file 2 has around 30000 lines : Expected Output has to be : hxd hcd jws (5 Replies)
Discussion started by: TestPractice
5 Replies

3. Programming

Find the minimum value of the column with respect to other column

Hi All, I would like get the minimum value in the certain column with respect to other column. For example, I have a text file like this. ATOM 1 QSS SPH S 0 -2.790 -1.180 -2.282 2.28 2.28 ATOM 1 QSS SPH S 1 -2.915 -1.024 -2.032 2.31 2.31 ATOM 1 ... (4 Replies)
Discussion started by: bala06
4 Replies

4. Shell Programming and Scripting

awk to find maximum and minimum from column and store in other column

Need your support for below. Please help to get required output If column 5 is INV then only consider column1 and take out duplicates/identical rows/values from column1 and then put minimum value of column6 in column7 and put maximum value in column 8 and then need to do subtract values of... (7 Replies)
Discussion started by: as7951
7 Replies

5. Shell Programming and Scripting

Find minimum and maximum values based on column with associative array

Hello, I need to find out the minimum and maximum values based on specific column, and then print out the entire row with the max value. Infile.txt: scf6 290173 290416 . + X_047241 T_00113118-1 scf6 290491 290957 . + X_047241 T_00113118-2 scf6 290898 290957 . + X_047241 T_00113119-3 scf6... (2 Replies)
Discussion started by: yifangt
2 Replies

6. Shell Programming and Scripting

Find minimum value different from zero

Hello, I have this file file1.csv Element1;23-10-2012;1,450;1,564;1,428 Element2;23-10-2012;1,448;1,565;1,427 Element3;23-10-2012;1,453;1,570;1,424 Element4;23-10-2012;1,428;1,542;1,405 Element5;23-10-2012;1,461;;1,453 Element6;23-10-2012;1,438;1,555;1,417... (6 Replies)
Discussion started by: saba01
6 Replies

7. UNIX for Dummies Questions & Answers

[Solved] Using awk to obtain minimum of each column (ignoring zeros)

Hi, I have a wide and long dataset which looks as follows: 0 3 4 2 3 0 2 2 ... 3 2 4 0 2 2 2 3 ... 0 3 4 2 0 4 4 4 ... 3 0 4 2 2 4 2 4 ... .... I would like to obtain the minimum of each column (ignoring zero values) so the output would look like: 3 2 4 2 2 2 2 2 I have the... (3 Replies)
Discussion started by: kasan0
3 Replies

8. Shell Programming and Scripting

Finding minimum value

Hi All, I have multiple files which contains 5 columns and multiple rows..... I want to calculate the minimum value of column 5th, if column 2 is MET, till column 1 comes to the next number. Also it must skip the condition similar to 1st line where column number 1 and 3 are same and... (9 Replies)
Discussion started by: CAch
9 Replies

9. Shell Programming and Scripting

Compare Two Files(Column By Column) In Perl or shell

Hi, I am writing a comparator script, which comapre two txt files(column by column) below are the precondition of this comparator 1)columns of file are not seperated Ex. file1.txt 8888812341181892 1243548895685687 8945896789897789 1111111111111111 file2.txt 9578956789567897... (2 Replies)
Discussion started by: kumar96877
2 Replies

10. UNIX for Dummies Questions & Answers

Getting the minimum of each column in a file

Hi, I have a file like: 0.000000 124.085533 124.085533 124.085533 124.085533 124.085533 124.085533 124.085533 124.085533 124.085533 33.097845 33.363764 0.000000 266.483441 262.519130 266.380993 274.989622 289.594799 309.523518 336.124848 372.386124 413.522043 429.984825 421.621810... (6 Replies)
Discussion started by: cosmologist
6 Replies
Login or Register to Ask a Question