LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 08-17-2006, 07:24 AM   #1
pwc101
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:
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

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

Rep: Reputation: 33
You could use a single tr:
Code:
ada@barnabas:~> echo "a<b>c|d" | tr "<>|" "   "
a b c d
 
Old 08-18-2006, 10:10 AM   #3
soggycornflake
Member
 
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!

Code:
perl -wne 's/^.*?<([^>]+)>\s+<([^>]+)>\s+<[^>]+>\s*$/$2 $1/&&print' <input file>
See, much more readable!
 
Old 08-18-2006, 11:23 AM   #4
pwc101
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):

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?...
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:38 PM.

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