Variable substitution in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable substitution in awk
# 1  
Old 04-13-2010
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:
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, the value of the variable REPL_DT is,
Code:
echo $REPL_DT
2010\\\/04\\\/12

The error I get in awk statement is as follows:
Code:
 Syntax Error The source line is 1.
 The error context is
                {sub ($4, >>>  2010\ <<< \\\\); printf "# %-7s %9s %18s\n", $2,$3,$4}
 awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.

When REPL_DT is only 2010 without any '\\\' it gets substituted correctly. I think the slashes after 2010 is causing problem. Any idea how to deal with this.

Thanks in advance,
Sugan.

---------- Post updated at 11:03 PM ---------- Previous update was at 09:41 PM ----------

I replaced the terminator in sed from '\' to '|' thinking it might help. Here is my piece of code:
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

It works but the problem is, instead of '2010/04/12', awk substitutes the value after dividing 2010/04 and then dividing the result by 12. It substitutes 41.83!!!

I tried substituting something like '2010\/04\/12' but it does not work in awk. Please help.

---------- Post updated 04-13-10 at 01:46 AM ---------- Previous update was 04-12-10 at 11:03 PM ----------

Hi,
I replaced the code a little to circumvent this problem. I used the following code and the problem is fixed...Smilie
Code:
      NEW_LINE=$(echo $Line | awk '{sub ($4, '$REPL_DT'); print}')
      r_yy=`echo $NEW_LINE | cut -d " " -f4 | cut -c1-4`/
      r_mm=`echo $NEW_LINE | cut -d " " -f4 | cut -c5-6`/
      r_dd=`echo $NEW_LINE | cut -d " " -f4 | cut -c7-8`
      r_line=`echo $NEW_LINE | cut -d " " -f1-3`
      REP_LINE=`echo "$r_line" "$r_yy""$r_mm""$r_dd"`
      NEW_LINE=`echo $REP_LINE | awk -F " " '{printf "# %-7s %9s %18s\n", $2,$3,$4}'`
      sed $n" s|.*|$NEW_LINE|" kfile > tmp
      mv tmp kfile

Thank you very much.
# 2  
Old 04-13-2010
Try something like this:
Code:
REPL_DT=`date +'%Y\/%m\/%d'`
NEW_LINE=$(echo $Line | awk -v var=$REPL_DT '{$4=var; printf "# %-7s %9s %18s\n", $2,$3,$4}')

# 3  
Old 04-13-2010
Hi,
Your code is working fine. Thanks so much.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk variable substitution problem

Hi I am having a file like this ############################## j=1 while ] do temp_5=MODULE$j awk ' $1 ~ /'${!temp_5}'/ { do something }1' file1 > file2 ((j = j +1 )) done ################### Setting the variables like this (8 Replies)
Discussion started by: kshitij
8 Replies

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

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

4. Shell Programming and Scripting

variable substitution

file1.ksh #!/bin/ksh test5_create="I am a man" # test5 will be dynamic and the value will be passed from command line a=${1}_create echo $a # i need the output as "I am a man" ./file1.ksh test5 # i run the script like this any suggessions guys... (1 Reply)
Discussion started by: giri_luck
1 Replies

5. UNIX for Dummies Questions & Answers

Variable substitution

Hi, That might be pretty simple. How can I generate a variable name and get their value ? Thanks a lot. Something like: >CUSTOMER_NF=26 > object=CUSTOMER > echo ${object}_NF CUSTOMER_NF > echo ${${object}_NF} ksh: ${${object}_NF}: 0403-011 The specified substitution is... (7 Replies)
Discussion started by: Leo_NN
7 Replies

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

7. Shell Programming and Scripting

Variable Substitution

I have run into a wall with my iptables firewall scripting. I am blocking all of the private side IP addresses on the WAN interface on systems running NAT. However, if the system is not running NAT and needs to allow access to the local LAN on the WAN interface, I need to block all but one of... (2 Replies)
Discussion started by: garak
2 Replies

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

9. UNIX for Dummies Questions & Answers

variable substitution

Hi everyone, I have a simple question to ask : In a script that I'm writting, I need to create variables on-the-fly. For instance, for every iterartion of the following loop a var_X variable should be generated : #!/bin/ksh a="1 2 3" for i in $a do var_${i}=$i echo "${var_$i}" done ... (1 Reply)
Discussion started by: ck-18
1 Replies

10. UNIX for Advanced & Expert Users

Substitution in a variable

Hey All, I'm trying to clean up a variable using sed but It dosn't seem to work. I'm trying to find all the spaces and replace them with "\ " (a slash and a space). For Example "Hello World" should become "Hello\ World". But it does nothing. If I put it directly into the command line it works... (3 Replies)
Discussion started by: spragueg
3 Replies
Login or Register to Ask a Question