1

I'm trying to extract the xy coordinates of some earthquake occurrences along with their magnitudes from a file "seismic_c_am.txt", and plot them as circles of various sizes and colours based on the magnitude. Here is what I have so far:

25  i=`awk '{ FS = "|" ; print $11}' seismic_c_am.txt`
26
27  if [ "$i" -gt 7 ] ; then
28    awk 'NR%25==0 { FS = "|" ; print $4, $3}' seismic_c_am.txt | psxy $rgn $proj -Sc0.25c -Gred -O -K >> $psfile ;
29  fi
30
31  if [ "$i" -gt 5 ] && [ "$i" -le 7 ] ; then
32  awk 'NR%25==0 { FS = "|" ; print $4, $3}' seismic_c_am.txt | psxy $rgn $proj -Sc0.2c -Gorange -O -K >> $psfile ;
33  fi
34
35  if [ "$i" -le 5 ] ; then
36  awk 'NR%25==0 { FS = "|" ; print $4, $3}' seismic_c_am.txt | psxy $rgn $proj -Sc0.1c -Gyellow -O -K >> $psfile ;
37  fi

This script seems to just print all the magnitudes ($11) into the terminal, and the last line reads:

.
.
3.6
4.0
1.7
3.6 : integer expression expected

But I don't know which line this is referring to! Possibly line 27, 31 or 35? (see above)

3
  • 6
    5.5 is not an integer, there's your problem. Same with the variables. You can't do floating-point maths in bash. Instead of calling awk so many times, you should try and combine the scripts together. Commented Nov 11, 2014 at 15:20
  • I've replaced the "5.5"s with "5"s, same issue. Does $i have to be an integer? Commented Nov 11, 2014 at 15:22
  • @co323 yes both $i and the other value has to be integer Commented Nov 11, 2014 at 15:30

1 Answer 1

2

Bash doesn't do floating point arithmetic, only integer arithmetic.

Since you're comparing with integers, you can make awk print the integer part.

i=`awk '{ FS = "|" ; printf "%d", $11}' seismic_c_am.txt`

If you want to know which line is causing these errors, add the command set -x to your script to turn on tracing mode: bash will print each script line before executing it. If you only want to trace part of the script, you can turn off tracing with set +x.

Since you're repeating the same snippet many times, you may want to restructure your script a bit.

i=`awk '{ FS = "|" ; printf "%d", $11}' seismic_c_am.txt`
if [ $i -ge 7 ]; then
  sc_value=0.25 color=red
elif [ $i -ge 5 ]; then
  sc_value=0.2 color=orange
else
  sc_value=0.1 color=yellow
fi
awk 'NR%25==0 { FS = "|" ; print $4, $3}' seismic_c_am.txt |
psxy $rgn $proj -Sc${sc_value}c -G$color -O -K >> $psfile
Sign up to request clarification or add additional context in comments.

4 Comments

Is it OK to do multiple assignments on the same line like that? Also, I'd probably go with $( instead of backticks. Otherwise, looks good.
@TomFenech Sure, you can have any number of assignments on the same line (whether or not they're followed by a command). I don't use backticks in my own script either, and I always use double quotes around variable substitutions, but I didn't want to change the script too much beyond the point I wanted to convey.
Thanks guys this is great input. Gilles that fixed it for me cheers
I do suggest you replace old and outdated back-tics with parentheses like this: i=$(awk 'code')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.