1

I'm trying to use a Bash script to run a large number of calculations (just over 2 million) using a terminal-based program called uvspec. But I've hit a serious barrier following the latest addition to the calculation...

The script, opens an input file which has 2e^6 lines looking like this:

0 66.3426 -9.999 -9999
0 66.6192 -9.999 -9999
0 61.9212 1.655 1655
0 61.9999 1.655 1655
...

Each of these values represents a different value I want to substitute into the input file (using sed), so I read each line into an array. Many of these lines contain negative values in the 4th column e.g. -9999, which result in errors in the program so I would like to omit those lines and return a standard output - I'm doing this with the if statement... Problem is something terribly wrong is coming out of my output and I'm 99.9% sure the problem is a mistake in the following script as I'm fairly new to bash.

Can anyone spot anything here that doesn't make sense or is bad syntax?

Any comments on the script in general would also be useful feedback.

cat ".../Maps/dniinput" | while IFS=$' ' read -r -a myArray
do  
if [ "${myArray[3]}" -gt 0 ]
    then
      sed s/TAU/"${myArray[0]}"/ x.template x.template > a.template
      sed s/SZA/"${myArray[1]}"/ a.template a.template > b.template
      sed s/ALT/"${myArray[2]}"/ b.template b.template > x.inp
       ../bin/uvspec < x.inp >> dni.out

 else
     echo "0 -9999" >> dnijul.out
 fi 
done
2
  • 1
    Can you be more specific about what the problem is? Are you referring to the fact that x.inp contains 8 copies of x.template? That's because you specify it twice for each sed Commented Dec 10, 2013 at 22:37
  • Basically each of those lines corresponds to a specific position on a latitude, longitude graph. The 4th column the altitude as an integer number of km. Altitudes of -9999 correspond to oceans. When I plot the altitudes in input file I get a very accurate world land map. What I want my script to do is return "0 -9999" for each of those locations without bothering with a calcuation, but when I plot my output file, the -9999s are jumbled up and in no way resemble a world map! Commented Dec 10, 2013 at 22:53

2 Answers 2

2

Sed can do all three substitutions in one go and you can pipe the output straight into your analysis program without creating any intermediate a.template and b.template files...

sed -e "s/.../.../" -e "s/.../.../" -e "s/.../.../" x.template | ../bin/uvspec

By the way, you can also get rid of the "cat" at the start, and replace your array with variables whose names better match what they are, if you use a loop like this:

while IFS=S' ' read tau sza alt p4
do
   echo $tau $sza $alt $p4
done < a
0 66.3426 -9.999 -9999
0 66.6192 -9.999 -9999
0 61.9212 1.655 1655
0 61.9999 1.655 1655

I named the fourth element "p4" because you refer to the 4th one as the altitude in your comment, but in your code you replace the word "ALT" with the third column - so I am not really sure what your parameters are, but you should hopefully get the idea from the example above.

Sign up to request clarification or add additional context in comments.

3 Comments

Ah nice. Cheers mate!
@shellter You'll notice that each of the different files is produced by the sed command before it.
Could you accept my answer with a lovely big green tick if it worked out for you? Or just say if you are still having problems and need further assistance.
0

You might want to combine those "sed" lines into something more like:

 sed -e "s/TAU/${myArray[0]}/" -e "s/SZA/${myArray[1]}/" \
     -e "s/ALT/${myArray[2]}/" < x.template \
   | ../bin/uvspec >> dni.out

Comments

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.