The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
read a variable character by character, substitute characters with something else vipervenom25 UNIX for Dummies Questions & Answers 2 06-06-2008 12:18 PM
comparing values of same variable coolkid Shell Programming and Scripting 2 05-27-2008 02:45 PM
getting values from variable in a loop kriuz Shell Programming and Scripting 3 01-22-2008 01:50 PM
assigning values to a variable trichyselva UNIX for Dummies Questions & Answers 3 12-13-2007 09:55 PM
Substitute Variable mmg2711 UNIX for Dummies Questions & Answers 0 10-26-2007 03:59 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-10-2006
Registered User
 

Join Date: Mar 2006
Posts: 15
Stumble this Post!
Substitute variable values

Hi,
I am trying to redefine the value of a variable based on another variable value. And I want to read in my variables from a enviroment file in the end -- at least I think so. But 1st here's what I want I need to get working:

var_1="11 10 9 8 7 6 5 4 3 2 1"
var_2=3
var_3=4

So I want var_4="1 2 5 6 7 8 9 10 11 12 13".

How do I extract var_2 & var_3 values from var_1 to create var_4?

Thanks,

John
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-10-2006
Registered User
 

Join Date: Jan 2005
Posts: 682
Stumble this Post!
Not very clear but awk can get you close:

print "11 10 9 8 7 6 5 4 3 2 1" | nawk '{gsub(/(3\ |4\ )/,"");print}'
Reply With Quote
  #3 (permalink)  
Old 03-10-2006
Registered User
 

Join Date: Jan 2005
Posts: 682
Stumble this Post!
A better version of the clumsy awk approach:

print "13 11 10 9 8 7 6 5 4 3 2 1" | nawk '{gsub(/ 3 /," ",$0);gsub(/ 4 /," ");print}'
Reply With Quote
  #4 (permalink)  
Old 03-10-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,002
Stumble this Post!
Code:
#!/bin/ksh

var_1="11 10 9 8 7 6 5 4 3 2 1"
var_2=3
var_3=4

var_4=$(echo "$var_1" | nawk -v v2="${var_2}" -v v3="${var_3}" '
{
   for(i=NF;i;i--)
     if( $i == v2 || $i == v3 )
       continue
     else
       printf("%s%s", $i, (i!=1) ? OFS : "\n")
}
')

echo "var_4->[${var_4}]"
Reply With Quote
  #5 (permalink)  
Old 03-10-2006
Registered User
 

Join Date: Mar 2006
Posts: 15
Stumble this Post!
Thanks,
I could not get the 1st oneto work but the 2nd version worked minus using awk instead of nawk (nawk not in the system).

But you hardcoded 3 & 4 in the gsub's and I need to have them as variables. What is getting substituted will change with every line read from my input file.

I'm new to this level of scripts so I will trying to enter variables in the 2nd version and see what happens.

Thanks again -- have a good weekend.
Reply With Quote
  #6 (permalink)  
Old 03-13-2006
Registered User
 

Join Date: Mar 2006
Posts: 15
Stumble this Post!
Substitue variable value

Quote:
Originally Posted by tmarikle
A better version of the clumsy awk approach:

print "13 11 10 9 8 7 6 5 4 3 2 1" | nawk '{gsub(/ 3 /," ",$0);gsub(/ 4 /," ");print}'

The above works but my finished script needs to have a variable in the gsub where the '3' & '4' are. I want to get these values from a input file.

I tried things like below but I get Invalid pattern.

print "13 11 10 9 8 7 6 5 4 3 2 1" | nawk '{gsub(/$var_2/,," ",$0);gsub(/ var_3/," ");print}'

What does this code need to be? Thanks
Reply With Quote
  #7 (permalink)  
Old 03-13-2006
Registered User
 

Join Date: Jan 2005
Posts: 682
Stumble this Post!
nawk -v var_2=$var_2 -v var_3=$var_3 '{gsub...
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:37 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0