0

I want to sum over floats of the form: 3.016e-06 The floats are saved in a files called S_3x3.txt S_4x4.txt ... S_8x8.txt My code sums only the first integer. Instead of 3.016e-06 + 4.022e-06 it calculates 3 + 4. How can I fix this?

 x=3 while [ $x -le 8 ] do find . -name "S_$((x))x$((x))*" -print | sort -t'_' -nk3 | xargs -J {} awk -F' ' 'FNR == 1 && !/^($|#)/{ sum+=$1) }END{ print sum }' {} >> cS_CPU.txt x=$(( x+1 )) done 
6
  • can you provide two input files ? with data and skipped lines ? you awk code will only collect data on forst line of every file (along with no '$' and no '#') is that what you want ? Commented Aug 31, 2015 at 14:33
  • 1
    "S_$((x))x$((x))*" -- that's a funny place to use arithmetic evaluation. Why not "S_${x}x${x}*" Commented Aug 31, 2015 at 14:38
  • 1
    are you sure the "-" in the exponent is not a lookalike char? pass through cat -vet to see. Commented Aug 31, 2015 at 14:40
  • works for me with gawk, nawk, mawk. What version of awk are you working with? Commented Aug 31, 2015 at 14:40
  • my version is: awk version 20070501 Commented Aug 31, 2015 at 15:42

1 Answer 1

0

not an answer, but too long for a comment

 x=3 while [ $x -le 8 ] do find . -name "S_${x}x${x}*" -print | sort -t'_' -nk3 | xargs -J {} awk 'FNR == 1 && !/^($|#)/ { sum+=$1) } END{ print sum }' {} >> cS_CPU.txt x=$(( x+1 )) done 
  • need for -F' ' ? awk will separate field by space
  • "S_$((x))x$((x))*" ? can be turn into "S_${x}x${x}*".
2
  • "S_$((x))x$((x))*" works fine as well ;) -F' ' is a remnant of an old code. It does not do anything here as I have one column only. My problem is to find the right format for the sum. $1 are all of the format 6.432e-07. How do I sum over them? Commented Aug 31, 2015 at 15:08
  • @JulianeKiqhwdf: $((x))x$((x)) may "work", but it is pointless (at best) to do an unnecessary numeric evaluation on a variable which you already know to be the number you want. Commented Aug 31, 2015 at 15:23

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.