awk variable substitution problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk variable substitution problem
# 1  
Old 05-13-2014
Wrench awk variable substitution problem

Hi

I am having a file like this

##############################
Code:
j=1
while [[ $j -le $COUNT ]]
do 
temp_5=MODULE$j

awk '
$1 ~ /'${!temp_5}'/ {
do something }1'  file1 > file2 

((j = j +1 ))

done

###################

Setting the variables like this

Code:
setenv MODULE1 mod1
setenv MODULE2 mod2 
setenv MODULE3 mod3

My problem is that the '${!temp_5}' substitution is not at all happening and with the help of awk can we write to the same file ...

If I am changing and giving variable like this '$MODULE1' then its taking ...
but I need to increment $j also to get the required variables
# 2  
Old 05-13-2014
Variable expansion will not happen if you use single quotes. Try using double quotes.
# 3  
Old 05-13-2014
Try:
Code:
awk -v t5="${!temp_5}" '$1 ~ t5 {do something }1'  file1 > file2

# 4  
Old 05-13-2014
awk variable substitution

I tried with double quotes "" but not working

Code:
j=1 while [[ $j -le $COUNT ]] do  temp_5=MODULE$j  awk ' $1 ~ /"${!temp_5}"/ { do something }1'  file1 > file2   ((j = j +1 ))  done

# 5  
Old 05-13-2014
You may also want to change the single quotes the awk. Not just the one for ${!temp_5}.
# 6  
Old 05-13-2014
Hi Scrutinizer ,

I tried what you suggested but its not working

Code:
j=1 

while [[ $j -le $COUNT ]]
do

  temp_5=MODULE$j

awk -v  t5="${!temp_5}" '$1 ~ t5 {
    add = 1
}
add && /[)];/ {
    sub(/[)];/, ",.DFT_ClkEnScanIn1 ( DFT_ClkEnScanIn1 ) , .scan_clock_en ( DFT_ClkEnScanOut1 ) , .DFT_ClkgenScanEnable ));")
    add = 0
}
1'   modem5_2_top_aapd_wrapper.prescan.v > temp20.v

((j = j +1))

done

---------- Post updated at 01:15 PM ---------- Previous update was at 01:09 PM ----------

Hi Chacko

tried with double quotes but getting syntax error

Code:
j=1 
while [[ $j -le $COUNT ]]
do
  temp_5=MODULE$j
awk -v  t5="${!temp_5}" "$1 ~ t5 {
    add = 1
}
add && /[)];/ {
    sub(/[)];/, ",.DFT_ClkEnScanIn1 ( DFT_ClkEnScanIn1 ) , .scan_clock_en ( DFT_ClkEnScanOut1 ) , .DFT_ClkgenScanEnable ));")
    add = 0
}
1"   modem5_2_top_aapd_wrapper.prescan.v > temp20.v

((j = j +1))

done


Last edited by Scrutinizer; 05-13-2014 at 04:56 AM..
# 7  
Old 05-13-2014
@kshitij: what is not working?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Awk: problem for loop through variable

Hi, input: AAA|1 my script (the function is just an example): gawk 'BEGIN{FS=OFS="|"} function repeat(str, n, rep, i){ for(i=1; i<=n; i++) rep=rep str return rep } { variable_1=repeat($1,$2) variable_2=repeat($1,$2+1) variable_3=repeat($1,$2+3) ... (5 Replies)
Discussion started by: beca123456
5 Replies

2. UNIX for Dummies Questions & Answers

Variable substitution problem

HI i was studying about variable substitution. below are info which was given in a online tutorial. ${parameter:-word}---> If parameter is null or unset, word is substituted for parameter. The value of parameter does not change. ${parameter:=word}---> If parameter is null or... (3 Replies)
Discussion started by: scriptor
3 Replies

3. Shell Programming and Scripting

Variable value substitution issue with awk command issue

Hi All, I am using the below script which has awk command, but it is not returing the expected result. can some pls help me to correct the command. The below script sample.ksh should give the result if the value of last 4 digits in the variable NM matches with the variable value DAT. The... (7 Replies)
Discussion started by: G.K.K
7 Replies

4. Shell Programming and Scripting

Problem using variable inside awk

HI, This is the code I am using: awk -v aaa="connect" 'BEGIN {IGNORECASE} /aaa/,/!/ {print NR}' bb This does not throw any error but it does not work. Pls help Thanks. (4 Replies)
Discussion started by: sudvishw
4 Replies

5. Shell Programming and Scripting

awk variable problem

Hi How are you people .. I am facing another problem regarding awk below is my code -bash-3.00$ export a=10 -bash-3.00$ echo $a 10 -bash-3.00$ echo "" | nawk -v var=$a ' { if(var=10) { print "abc" } else { print "def"} } ' abc -bash-3.00$ echo "" | nawk -v var=$a ' { if(var==10)... (2 Replies)
Discussion started by: aishsimplesweet
2 Replies

6. Shell Programming and Scripting

How to use variable with command substitution in variable

For example I have variable like below echo $OUTPUT /some/path/`uname -n` when I try to use the variable OUTPUT like below cd $OUTPUT or cd ${OUTPUT} I am getting bad substituion error message $ cd $OUTPUT ksh: cd: bad substitution $ cd ${OUTPUT} ksh: cd: bad substitution ... (1 Reply)
Discussion started by: rajukv
1 Replies

7. Shell Programming and Scripting

Variable substitution in awk

Hi, I have a variable to be substituted in awk. I am using AIX 5.3. Here is my piece of code: REPL_DT=`date +'%Y\\\\\\\\\/%m\\\\\\\\\/%d'` NEW_LINE=$(echo $Line | awk '{sub ($4, '$REPL_DT'); printf "# %-7s %9s %18s\n", $2,$3,$4}') sed $n" s/.*/$NEW_LINE/" kfile > tmp mv tmp kfile Here,... (2 Replies)
Discussion started by: sugan
2 Replies

8. Shell Programming and Scripting

sed variable substitution problem

Hi, I am facing a strange problem. I have a script that used the following to search and replace text: sed 's/'"${find_var_parm}"'/'"${find_var_filter}"'/g' $ParmFile > $TempFile The values of $find_var_parm and $find_var_filter are set based on search criteria. The above seems to be working... (2 Replies)
Discussion started by: arsh
2 Replies

9. Shell Programming and Scripting

Sed variable substitution when variable constructed of a directory path

Hello, i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem. my var1 is a string constructed like this: filename1 filerev1 filepath1 my var2 is another string constructed like this: filename2 filerev2 filepath2 when i do... (2 Replies)
Discussion started by: alrinno
2 Replies

10. UNIX for Dummies Questions & Answers

awk variable substitution

for the command below, it looks for the 3rd field value matching "P" and printing it. awk '{if ($3 == "P") print}' file how would i express this if i use a loop to find more that 1 variable fro a list? this doesn't seem to work... cat list | while read n do awk '{if ($3 == "$n") print}'... (1 Reply)
Discussion started by: apalex
1 Replies
Login or Register to Ask a Question