LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   setting a variable variable in a script... this works, but could it be more elegant? (https://www.linuxquestions.org/questions/programming-9/setting-a-variable-variable-in-a-script-this-works-but-could-it-be-more-elegant-474726/)

pwc101 08-17-2006 07:24 AM

setting a variable variable in a script... this works, but could it be more elegant?
 
Hello everyone,

For my research project I have to plot a lot of graphs. We use a software package called Generic Mapping Tools (GMT) for this, where you create csh scripts which call a number of gmt programs which plot to postscript files.

I've created a script which uses a foreach loop to plot a number of graphs (about 50 in total) which creates certain variables each time the loop runs. For each iteration, the command I want people's opinions on calculates the minimum and maximum value of a number of columns for each individual input file.

Each input file looks something like this:
Code:

  Eastings:        Northings:      Depth:          Distance Along Line:  Spacing:
  581294.06604    93871.6534648  -28.8670818761                0          1
  581294.10565    93869.7182042  -28.9322498873    1.93566588256          1
... 
  581319.851135    92611.8496475  -31.3056972753    1260.06766847          1
  581319.891719    92609.8667921    -31.321087542    1262.05093919          1

Part of the GMT package contains a function called minmax, which displays the minimum and maximum values of each column:
Code:

3d_dunes_001_ns.dat: N = 645    <581294/581320> <92609.9/93871.7>      <-31.8896/-28.7709>    <0/1262.05>    <1/1>
The variable I need to create must have the minimum and maximum value for the 4th and 3rd columns of the input file, formatted as follows:
Code:

0/1262.05/-31.8896/-28.7709
What I've got (which works, but seems inelegant to me), is:
Code:

set plot_area=-R`cat 3d_dunes_001_ns.dat | minmax | tr "/" " " | tr "<" " " | tr ">" " " | awk '{printf "%1d %4d %2d %2d\n", $12,($13+50),($10-2),($11+2)}' | tr " " "/"`
where the tr's replace all the '<','>' and '/' with spaces, which are then made a fixed width in awk, and then correctly ordered (min_depth/max_depth/min_dist/max_dist) and a value of 2 added to each value, to buffer the extents of the plot.

So basically, I would like to know if there's a more elegant way of getting rid of the '<','>' and '/' without using 3 tr's?

Thanks :)

spirit receiver 08-17-2006 07:33 AM

You could use a single tr:
Code:

ada@barnabas:~> echo "a<b>c|d" | tr "<>|" "  "
a b c d


soggycornflake 08-18-2006 10:10 AM

Holy unreadable pipelines Batman! This is a job for.... Perl!

Code:

perl -wne 's/^.*?<([^>]+)>\s+<([^>]+)>\s+<[^>]+>\s*$/$2 $1/&&print' <input file>
See, much more readable! ;)

pwc101 08-18-2006 11:23 AM

heh! this is my most recent monster command (can't be bothered to put it into a script, so just run it as is from the cli):

Code:

set input = raw_data/bank_parallel_002.dat && set minmax = -36/-33 && awk '{print $4, $3}' $input | psxy -JX15/3 -R0/400/$minmax -Ba200g100/a1g0.5 -Xc -Y25 -P -K > test.ps && awk '{print $4, $3}' $input | psxy -JX15/3 -R400/800/$minmax -Ba200g100/a1g0.5 -Y-6 -P -O -K >> test.ps && awk '{print $4, $3}' $input | psxy -JX15/3 -R800/1200/$minmax -Ba200g100/a1g0.5 -Y-6 -P -O -K >> test.ps && awk '{print $4, $3}' $input | psxy -JX15/3 -R1200/1400/$minmax -Ba200g100/a1g0.5 -Y-6 -P -O -K >> test.ps && gs -sPAPERSIZE=a4 test.ps
elegant, eh?... ;)


All times are GMT -5. The time now is 05:00 AM.