Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to use external variable in awk

Status
Not open for further replies.

bsmile001

Programmer
Aug 23, 2011
1
US
it's not about using variables defined in awk with awk, but using variables defined in another software which makes use of awk to do string treatment, say within gnuplot

I need to do something like this, use awk to filter out data within a specific range whose limit is externally defined, say

plot "< awk ' ($1<s1) || ($1>s2) { print $1, $3 } ' fort.47" u 1:2 w l

here s1, s2 are variables defined in gnuplot to specify lower and upper limit, plot is a command in gnuplot.

The above does not achieve my goal, how can I possibly improve it? Thanks,
 
Something like this perhaps?

Code:
echo plot $(awk ' ($1<s1) || ($1>s2) { print $1, $3 } ' fort.47) u 1:2 w l | gnuplot

Annihilannic.
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Hi,

To use variable you can make like that :
Code:
awk ' ($1<s1) || ($1>s2) { print $1, $3 } ' s1=$s1 s2=$s2 fort.47

It is not very clean because you use the same name for the variable outside and inside awk. I think it is better like this :

Code:
awk ' ($1<var1) || ($1>var2) { print $1, $3 } ' var1=$s1 var2=$s2 fort.47

Regards
jmc0031
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top