Problem using shell variable in awk if condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem using shell variable in awk if condition
# 1  
Old 05-30-2012
Network Problem using shell variable in awk if condition

Hi friends,

I'm having a bit of a problem using shell variable in an awk if statement.

Please note that i'm using -v option as listed in many forums but I still don't get it working. Here's my code. Kindly help as I've gone crazy trying to work this out Smilie

Code:
#!/bin/bash -xv

fnStats()
{
modes=("${!1}")
services=("${!2}")
dates=("${!3}")
prices=("${!4}")
for (( i = 0 ; i < ${#dates[@]} ; i++ )) do
echo "${dates[$i]}" > /home/sm_stats.txt
for (( j = 0 ; j < ${#services[@]} ; j++ )) do
for (( k = 0 ; k < ${#modes[@]} ; k++ )) do
for (( l = 0 ; l < ${#prices[@]} ; l++ )) do
a=$(grep "CHG=1,${prices[$l]}" TLOG_BILLING_REALTIME_${dates[$i]}* | awk -v s="${services[$j]}" -v m="${modes[$k]}" 'BEGIN {FS = "|"} {if($8=="A" && $7==s && $11==m) print $4}' | wc -l)
echo -n "$a," >> /home/sm_stats.txt
#echo "${dates[$i]} ${services[$j]} ${modes[$k]} ${prices[$l]} $a"
#echo "TLOG_BILLING_REALTIME_${dates[$i]}*"
done
done
done
done
}

cd /SUBMGR/Tomcat/logs/TLOG/BILLING_REALTIME

modes=(OBD VP WEB SMS PRESSSTAR)
services=(RBT_ACT_DEFAULT RBT_ACT_COPY RBT_ACT_GIFT)
prices=(5.0 2.5 1.0)
dates=(20120529)

fnStats modes[@] services[@] dates[@] prices[@]

In the code, i'm trying to get values from different loops and substituting it in the awk part of the code.

Infact, i turned on the debug option of bash and could see the below output when it was executing that particular line of code.

Code:
grep "CHG=1,${prices[$l]}" TLOG_BILLING_REALTIME_${dates[$i]}* |  awk -v s="${services[$j]}" -v m="${modes[$k]}" 'BEGIN {FS = "|"}  {if($8=="A" && $7==s && $11==m) print $4}' | wc -l
++ awk -v s=RBT_ACT_GIFT -v m=PRESSSTAR 'BEGIN {FS = "|"} {if($8=="A" && $7==s && $11==m) print $4}'
++ wc -l
++ grep CHG=1,1.0 TLOG_BILLING_REALTIME_20120529_000000..log

As you see above, it substitutes the corresponding loop variable value in the first part of the statement, but doesn't do so in the "if" condition!

I am suspecting the single quote before 'BEGIN' which i think needs to be escaped. But i've tried that only to get a syntax error Smilie

Last edited by Scrutinizer; 05-30-2012 at 05:52 PM.. Reason: changed quote tags to code tags
# 2  
Old 05-30-2012
s and m are not supposed to be substituted by bash. What happens within awk can not be made visible by the shell debug options.
# 3  
Old 05-30-2012
Thanks for helping Scrutinizer.

In that case, can you please let me know what else could be the problem since i'm not getting the desired output?
# 4  
Old 05-30-2012
I don't know, could you post an input sample and a corresponding (desired) output sample and what you are trying to achieve?
# 5  
Old 05-30-2012
Never mind friend. Thanks a lot for your time.

I figured out that the pipe '|' was causing the problem. So, i redirected the output of grep to a file and used that file as an input to awk.

It seems to be working now.

Having said that, still trying to figure out why pipe "|" is not working.

Cheers Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Problem with getting awk to multiply a field by a value set based on condition of another field

Hi, So awk is driving me crazy on this one. I have searched everywhere and read man, docs and every related post Google can find and still no luck. The actual files I need to run this on are sensitive in nature, but it is the same thing as if I needed to calculate weighted grades for multiple... (15 Replies)
Discussion started by: cotilloe
15 Replies

2. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

3. Shell Programming and Scripting

How to pass IF condition via shell varibale in awk?

Hello, I have one query regarding passing IF condition shell variable inside awk. Here is the case- File content of keydefn.exp 201~2~LM Limit 02-current value~Limit 02 ~Limit02~Current~Value ~N~Y~S~0~9999999 201~3~LM Limit 03-current value~Limit... (2 Replies)
Discussion started by: anillambait
2 Replies

4. Shell Programming and Scripting

How to add second variable in awk nested if condition?

Hi Gurus, I have a command to assign value based on input value. current condition is "if pattern matches "case", then assign "HOLD" else "SUCC"right now, I need to add one more condition (variable name is VAR). the condition is "if pattern1 matches "case", then assign "HOLD" else if... (2 Replies)
Discussion started by: ken6503
2 Replies

5. Shell Programming and Scripting

Shell variable as awk condition

Hi, I'm trying to automate part of a script which uses awk to grab out some lines of a log file based on certain fields matching. For example, my log file looks something like the following (but 1000s of lines): 1 Tom 123 abc 345 2 Dick 345 abc 678 3 Harry 567 abc 345 4 Tom 345 cde 345... (3 Replies)
Discussion started by: chrissycc
3 Replies

6. Shell Programming and Scripting

If else condition inside for loop of awk command in UNIX shell scripting

Hi , Please excuse me for opening a new thread i am unable to find out the syntax error in my if else condition inside for loop in awk command , my actual aim is to print formatted html td tag when if condition (True) having string as "failed", could anyone please advise what is the right... (2 Replies)
Discussion started by: karthikram
2 Replies

7. Shell Programming and Scripting

Problem with awk array when loading from shell variable

Hi, I have a problem with awk array when iam trying to use awk in solaris box as below..Iam unable to figure out the problem.. Need your help. is there any alternative to make it in arrays from variable values nawk 'BEGIN {SUBSEP=" "; split("101880|110045 101887|110045 101896|110045... (9 Replies)
Discussion started by: cskumar
9 Replies

8. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

9. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

10. Shell Programming and Scripting

Problem with IF condition in awk

Hi all; I'm stuck with this simple awk script,i need to group the lines which the position of 28 length 3 that contains "688" into 1 group and other than "688" into another group. My problem is the script only read other than "688" and ignores the lines which contains "688". The file look... (2 Replies)
Discussion started by: ashikin_8119
2 Replies
Login or Register to Ask a Question