Greping numbers with dot in it

Hi,

I wanted to create a script what would take two numbers out of two files and add them together, but I got stuck with greping numbers what have a dot in it.

So far I have grepped the two lines what include the numbers I need (from both files) to a third file and from that file I try to grep only the numbers.

The third file looks like this:

aoforce.out:         *  zero point VIBRATIONAL energy  :      0.6725445  Hartree  *
job.last:                 |  total energy      =  -3842.57456191055  |

The script I have at the moment (thanks to your forums :o ) is like this:

grep -ho '[-0-9]\{1,20}' 

and with that script I get an output like this:

0
6725445
-3842
57456191055

But I would like an output like this:

0.6725445
-3842.57456191055
```[/i]


Thank you in advance,

Mario

Please use

```text
 and 
```

tags when posting code, data or logs, ty.

Are those numbers always at the same position in the files or does it vary?

Yes they are, but sometimes file "job.last" number varies. In my example it had four numbers before the dot but sometimes it has three or even two. Behind the dot there are always eleven numbers (and seven in aoforce.out).

I thought more field wise as used in awk, example:

*  zero point VIBRATIONAL energy  :      0.6725445  Hartree  *

# where

* = field1
zero = field2
point = field3
VIBRATIONAL = field4
energy = field5
: = field6
0.6725445 = field7
Hartree = field8
* = field9

Is the number in file aoforce.out always at field7 or can this vary? Same question for the other file.

Yes, they both are always on the same field.

If it is that constant then it is very easy:

awk 'NR==FNR {print $7; next} {print $5}' aoforce.out job.last
0.6725445
-3842.57456191055

Try it out.

It works, thank you very much :smiley: !

I must get more acquaintanced with the awk command.

Alternatively, a perl solution could be:

$
$ cat f1
aoforce.out:         *  zero point VIBRATIONAL energy  :      0.6725445  Hartree  *
job.last:                 |  total energy      =  -3842.57456191055  |
$
$ perl -lne 'chomp; @x=split; foreach $i(@x){if ($i =~ /\d+/){$s += $i; print $i}}END{print "Sum = ",$s}' f1
0.6725445
-3842.57456191055
Sum = -3841.90201741055
$
$

tyler_durden