View the Most Wanted LQ Wiki articles.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


Search this Thread
Old 08-17-2006, 07:24 AM   #1
Senior Member
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
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:
   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:
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:
What I've got (which works, but seems inelegant to me), is:
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?


Last edited by pwc101; 08-17-2006 at 07:25 AM.
Old 08-17-2006, 07:33 AM   #2
spirit receiver
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
You could use a single tr:
ada@barnabas:~> echo "a<b>c|d" | tr "<>|" "   "
a b c d
Old 08-18-2006, 10:10 AM   #3
Registered: May 2006
Location: England
Distribution: Slackware 10.2, Slamd64
Posts: 249

Rep: Reputation: 31
Holy unreadable pipelines Batman! This is a job for.... Perl!

perl -wne 's/^.*?<([^>]+)>\s+<([^>]+)>\s+<[^>]+>\s*$/$2 $1/&&print' <input file>
See, much more readable!
Old 08-18-2006, 11:23 AM   #4
Senior Member
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Original Poster
Rep: Reputation: 128Reputation: 128
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):

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 > && awk '{print $4, $3}' $input | psxy -JX15/3 -R400/800/$minmax -Ba200g100/a1g0.5 -Y-6 -P -O -K >> && awk '{print $4, $3}' $input | psxy -JX15/3 -R800/1200/$minmax -Ba200g100/a1g0.5 -Y-6 -P -O -K >> && awk '{print $4, $3}' $input | psxy -JX15/3 -R1200/1400/$minmax -Ba200g100/a1g0.5 -Y-6 -P -O -K >> && gs -sPAPERSIZE=a4
elegant, eh?...


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
environment variable setting chii-chan Linux - General 5 01-23-2009 07:29 PM
Scripting: accessing a variable stored in a variable? tomolesonjr Linux - Newbie 5 05-05-2006 08:47 PM
Retrieving from a variable whose name is inside a variable. thekillerbean Linux - General 4 02-09-2006 08:50 PM
setting environment variable kb_ganesh Ubuntu 3 09-10-2005 01:58 PM
How to get 'Window' variable from a 'Widget' variable bordel Programming 0 11-19-2003 03:19 AM

All times are GMT -5. The time now is 11:31 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration