Substitute variable values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Substitute variable values
# 1  
Old 03-10-2006
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
# 2  
Old 03-10-2006
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}'
# 3  
Old 03-10-2006
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}'
# 4  
Old 03-10-2006
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}]"

# 5  
Old 03-10-2006
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.
# 6  
Old 03-13-2006
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
# 7  
Old 03-13-2006
nawk -v var_2=$var_2 -v var_3=$var_3 '{gsub...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To substitute multiple variable by their content in a file

Dear All, I would like to instantiate a "pattern file" substituting "variable name" by their content. Saying, we have: 1/ a "pattern file" containing different "variable name", the first character of the "variable name" is "@": $ cat TPTModl.txt DEFINE... (12 Replies)
Discussion started by: dae
12 Replies

2. Shell Programming and Scripting

Substitute variable inside nawk

Hi, I need to set "prd" in the below command to a unix variable nawk '/^#/ {next} FNR==NR {prd;next} !($0 in prd)' So, this is what i did fname=prd // unix shell variable nawk -v fname=$fname '/^#/ {next} FNR==NR {fname;next} !($0 in fname)'But the value of fname i.e "prd" is not... (8 Replies)
Discussion started by: mohtashims
8 Replies

3. Shell Programming and Scripting

How to substitute variable in sed for special character?

Hi , I have input file like below Hi this is "vinoth". Hi happy to work with 'unix' USA(united states of America) My script variables are below : Dquote=Ộ Squote=&#$567 Obrac=&^986 Cbrac=&^745 I want to read the variables in my SED command to replace the double quote,single... (9 Replies)
Discussion started by: vinothsekark
9 Replies

4. Shell Programming and Scripting

AWK variable substitute issue

dear, I have below file called folderlist.txt # ParentFolder environment_flag SubFolders triss 1 checksum bookstructure 1 fx 1 checksum_GMDB I have a script which which will create the folders under... (3 Replies)
Discussion started by: manas_ranjan
3 Replies

5. Shell Programming and Scripting

awk substitute variable value in nth field

I have a large csv file that looks like this: The 3rd field is a unix time stamp that I want to convert to human readable. I wrote a bash script with this code: IFS=$',' cat $1 | while read ID user DATE text flags read; do echo -e "$ID,$user,$(date -d @$DATE),$text,$flags,$read... (3 Replies)
Discussion started by: stumpyuk
3 Replies

6. Shell Programming and Scripting

substitute variable in bash

hi all, Assume that i a having the following three lines in an executable file #/bin/bash a=Tue Tue=1 When i give echo $a the value should be 1, how to do this. Your suggestions please. Thanks in advance, Anish (4 Replies)
Discussion started by: anishkumarv
4 Replies

7. Shell Programming and Scripting

substitute variable for values in perl

hi all, how do i assign values passed in from command line to and sql statement in perl ?? e.g i want to assign :name1 and :Name2 to be whatever is passed into the perl script command line my $sqlStr = "select * from test_table where column1 = upper(nvl(:name1, name1 )) and column2... (1 Reply)
Discussion started by: cesarNZ
1 Replies

8. Shell Programming and Scripting

sed doubt - search and substitute string from variable.

hi, trying to learn more abt sed :( i want to substitute a variable(a) with other variable(b) appended. Read.txt contains: home/test2/abc home/test/root1 input.txt contains: make test "home/test1/none"version="1.3" wt's wrong test "home/test2/abc"version="1.0" make save... (9 Replies)
Discussion started by: dragon.1431
9 Replies

9. Shell Programming and Scripting

Using Awk to efficiently substitute values using 3 vectors

I'm trying to efficiently combine the fields of two vectors (vectors b and c) into a new vector (vector d) as defined by instructions from a 3rd vector (vector a). So vector a has either a 1 or 2 in each field specifying which vector (b or c respectively) should go into that field. Vector a is... (4 Replies)
Discussion started by: LaTortuga
4 Replies

10. UNIX for Dummies Questions & Answers

Substitute Variable

Can anyone tell me what is the purpose of a substitute variable in the unix programming language and give an example where it may be used? Thanks! (0 Replies)
Discussion started by: mmg2711
0 Replies
Login or Register to Ask a Question