Visit Our UNIX and Linux User Community


Grep for the most negative number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep for the most negative number
# 1  
Old 03-29-2012
Grep for the most negative number

Hello, I am trying to write a bash script which will give me the most negative number. Here is an example input:
Code:
 Ce         3.7729752124       -4.9505731588       -4.1061257680
 Ce        -6.9156611391       -0.5991784762        7.3051893138
 Ce         7.6489739875        0.3513020731       -4.1428887409
  O        -2.0367674927        6.4059113030        4.2342869120
  O         1.8496529300        6.8212696160        0.4577675608

I used
Code:
grep -- "-*"

which returns all lines with negative numbers, what I really need is for it to look and find (in this instance) -6.9156611391. Then add +6.9156611391 to all the numbers in the file ex.
Code:
Ce         3.7729752124       -4.9505731588       -4.1061257680

to
Code:
Ce        10.6886363515       1.9650879...          2.80953537...

All lines do not necessarily contain number nor do they start with Ce or O,
however all numbers that need changed have the 3 set format (x,y,z coordinates)

Thanks in advance

Last edited by Scrutinizer; 03-29-2012 at 01:44 PM.. Reason: please Use [code] tags [/code], thank you..
# 2  
Old 03-29-2012
Hi sdl27789,

One way using perl:
Code:
$ cat infile
 Ce         3.7729752124       -4.9505731588       -4.1061257680
 Ce        -6.9156611391       -0.5991784762        7.3051893138
 Ce         7.6489739875        0.3513020731       -4.1428887409
  O        -2.0367674927        6.4059113030        4.2342869120
  O         1.8496529300        6.8212696160        0.4577675608
$ cat script.pl
use warnings;
use strict;
use List::Util qw[ min ];

die qq[Usage: perl $0 <input-file>\n] unless @ARGV == 1;

push @ARGV, $ARGV[0];

my ($negative_num, $positive_num);

while ( <> ) {
        chomp;
        my @f = split;
        shift @f;
        my $min = min @f;
        $negative_num = !defined $negative_num || $min < $negative_num ? $min : $negative_num;
}
continue {
        last if eof;
}

$positive_num = abs $negative_num;

while ( <> ) {
        chomp;
        my @f = split;
        for my $pos ( 1 .. $#f ) {
                $f[ $pos ] += $positive_num;
        }

        printf qq[%s\n], join qq[\t], @f;
}
$ perl script.pl infile
Ce      10.6886363515   1.9650879803    2.8095353711
Ce      0       6.3164826629    14.2208504529
Ce      14.5646351266   7.2669632122    2.7727723982
O       4.8788936464    13.3215724421   11.1499480511
O       8.7653140691    13.7369307551   7.3734286999

# 3  
Old 03-29-2012
And another -

Code:
$
$ cat f66
Ce 3.7729752124 -4.9505731588 -4.1061257680
Ce -6.9156611391 -0.5991784762 7.3051893138
Ce 7.6489739875 0.3513020731 -4.1428887409
O -2.0367674927 6.4059113030 4.2342869120
O 1.8496529300 6.8212696160 0.4577675608
$
$
$ perl -ane 'push @x, [@F];
            map {$min=$x[$.-1]->[$_] if $x[$.-1]->[$_] < $min} (1..$#F);
            END {
              foreach $i (@x) {print join(" ", map{$_==0 ? $i->[$_] : $i->[$_]-$min} (0..$#{$i})),"\n"}
            }' f66
Ce 10.6886363515 1.9650879803 2.8095353711
Ce 0 6.3164826629 14.2208504529
Ce 14.5646351266 7.2669632122 2.7727723982
O 4.8788936464 13.3215724421 11.1499480511
O 8.7653140691 13.7369307551 7.3734286999
$
$

tyler_durden
# 4  
Old 03-29-2012
Code:
grep "-"

was sufficient. The * didn't do what you think it did -- it would've matched any number of dashes in a row.

To get the most negative number:

Code:
awk '{ for(N=2; N<=NF; N++) if((!MIN)||($N < MIN)) MIN=$N } END { print MIN }' filename

Expanding on that, this finds the minimum then subtracts from all lines:

Code:
awk -v OFS="\t" '
# Run this for all lines in the first given file
NR==FNR { for(N=2; N<=NF; N++) if((!MIN)||($N < MIN)) MIN=$N; next }
# Run this for all lines in any following files
{ for(N=2; N<=NF; N++) $N -= MIN; } 1' inputfile inputfile > outputfile

Yes, the same file is listed twice intentionally. The first time it reads it, it finds the minimum; the second time, it subtracts the minimum and prints.

It gives me this output:
Code:
Ce      10.6886 1.96509 2.80954
Ce      0       6.31648 14.2209
Ce      14.5646 7.26696 2.77277
O       4.87889 13.3216 11.1499
O       8.76531 13.7369 7.37343

Keep in mind that this can't write to the same file it's reading from.

Last edited by Corona688; 03-29-2012 at 02:24 PM..
# 5  
Old 03-29-2012
All are close to working but not eactly

Thank you for the replies, it almost works...
In the first reply,
0 needs to comeback as 0.0000000

in the second reply I received two strange parts in the file, one, the line
O 11.4730666365 0.0845493182000006 7.1215466626 with several extra decimal points
Two, a line O 12.69964795 0 12.7680007771 with no z coordinate.

In the third reply the decimal points end at 5 places.

I should add the text file is several thousand lines long so the demo values I gave are not all of it nor the lowest value
# 6  
Old 03-29-2012
This will give you 10 decimal places:

Code:
awk -v OFS="\t" '
# Run this for all lines in the first given file
NR==FNR { for(N=2; N<=NF; N++) if((!MIN)||($N < MIN)) MIN=$N; next }
# Run this for all lines in any following files
{ for(N=2; N<=NF; N++) $N =sprintf("%.10f", $N-MIN); } 1' inputfile inputfile > outputfile

But awk math is floating point, not infinite precision.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 03-29-2012
Fixed my script (modifications in red):
Code:
 $ cat script.pl
use warnings;
use strict;
use List::Util qw[ min ];

die qq[Usage: perl $0 <input-file>\n] unless @ARGV == 1;

push @ARGV, $ARGV[0];

my ($negative_num, $positive_num);

while ( <> ) { 
        chomp;
        my @f = split;
        shift @f; 
        my $min = min @f; 
        $negative_num = !defined $negative_num || $min < $negative_num ? $min : $negative_num;
}
continue {
        last if eof;
}

$positive_num = abs $negative_num;

while ( <> ) { 
        chomp;
        my @f = split;
        for my $pos ( 1 .. $#f ) { 
                $f[ $pos ] += $positive_num;
        }   

         #printf qq[%s\n], join qq[\t], @f;        
         printf qq[%s\t%.10f\t%.10f\t%.10f\n], @f; 
}
$ perl script.pl infile
Ce      10.6886363515   1.9650879803    2.8095353711
Ce      0.0000000000    6.3164826629    14.2208504529
Ce      14.5646351266   7.2669632122    2.7727723982
O       4.8788936464    13.3215724421   11.1499480511
O       8.7653140691    13.7369307551   7.3734286999

This User Gave Thanks to birei For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #801
Difficulty: Medium
The CSS z-index property specifies the stack order of an element.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Converting negative number to positive in a file

Hi ALL, I am having semi column separated file as below. I am having negative values for the records starting with 11095. How can I convert that positive number I tried this below seems not working sed 's/ \(*\)$/ -\1/;t;s/\(.*\)-/\1/ myfile myfile... (6 Replies)
Discussion started by: arunkumar_mca
6 Replies

2. Shell Programming and Scripting

Negative number comparison using nawk on Linux

Hi All, I am trying to compare two negative numbers using awk on linux.But it is giving me wrong result.Same code is working perfectly on solaris. print ((0+new_price) < MIN_PRICE) e.g If I try to compare -1.32(new_price) and -500(min_price) using "<" operator, output is 1 i.e true. ... (5 Replies)
Discussion started by: Rashmee
5 Replies

3. Shell Programming and Scripting

Grep negative matching

is it possible to reverse the output of grep -o (9 Replies)
Discussion started by: squrcles
9 Replies

4. Shell Programming and Scripting

Is it possible to Divide a negative number in bash script

I am using a small script to divide some numbers in a given file and display the output in another file. I am getting the following error basename: invalid option -- '5' Try `basename --help' for more information. (standard_in) 1: syntax error The script is : #!/bin/bash for i in `cat... (4 Replies)
Discussion started by: kmnr877
4 Replies

5. Shell Programming and Scripting

Print smallest negative number with corresponding index from a column

considering the following table: ID col1 col2 col3 col4 1 -16.06801249 13.49785832 -56.57087607 -27.00500526 2 -1.53315720 0.71731735 -42.03602078 -39.78554623 3 -1.53315190 0.71731587 -42.03601548 ... (3 Replies)
Discussion started by: Birda
3 Replies

6. Shell Programming and Scripting

Taking largest (negative) number from column of coordinates and adding positive form to every other

Hello all, I'm new to the forums and hope to be able to contribute something useful in the future; however I must admit that what has prompted me to join is the fact that currently I need help with something that has me at the end of my tether. I have a PDB (Protein Data Bank) file which I... (13 Replies)
Discussion started by: crunchgargoyle
13 Replies

7. HP-UX

Division returning a negative number

Hi All, Just faced an interesting thing in HP-UX. I was dividing 2955334616 / 2 by using echo `expr 2955334616 / 2` , and this ofcourse which expects 1477667308 to be returned. But I am getting -669816340 and I am :wall: how exactly this is possible. It is not one of the compliments (Ones or... (4 Replies)
Discussion started by: mail2sanand
4 Replies

8. Shell Programming and Scripting

ignore negative number in script

Hi, does anyone know how to ignore whether a number is negative in a script. E.g. if I have a variable that contains -1200, how do I ignore the minus sign? (1 Reply)
Discussion started by: borderblaster
1 Replies

9. Shell Programming and Scripting

Grep for a negative number

Hi, I want to search for a return code of -3. Using grep "-3" *.* is giving a syntax error. Please suggest as to how can we search for this pattern using grep. Thanks, Krishna (2 Replies)
Discussion started by: kzmatam
2 Replies

10. Programming

C++ how to isdigit() a negative number?

hi people, I have a function which I am passing a stream which is basically postfix notation if(isdigit(in.peek())) { in >> number; nums.push(number); } else if (strchr("+-*/", in.peek( )) != NULL) { in >> symbol; do_operation(symbol, nums, okay); } ... (1 Reply)
Discussion started by: Darklight
1 Replies

Featured Tech Videos