Sponsored Content
Top Forums Shell Programming and Scripting Problem using shell variable in awk if condition Post 302649025 by vishwas.s on Wednesday 30th of May 2012 04:49:54 PM
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
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
set_color(1)							       fish							      set_color(1)

NAME
set_color - set_color - set the terminal color set_color - set the terminal color Synopsis set_color [-v --version] [-h --help] [-b --background COLOR] [COLOR] Description Change the foreground and/or background color of the terminal. COLOR is one of black, red, green, brown, yellow, blue, magenta, purple, cyan, white and normal. o -b, --background Set the background color o -c, --print-colors Prints a list of all valid color names o -h, --help Display help message and exit o -o, --bold Set bold or extra bright mode o -u, --underline Set underlined mode o -v, --version Display version and exit Calling set_color normal will set the terminal color to whatever is the default color of the terminal. Some terminals use the --bold escape sequence to switch to a brighter color set. On such terminals, set_color white will result in a grey font color, while set_color --bold white will result in a white font color. Not all terminal emulators support all these features. This is not a bug in set_color but a missing feature in the terminal emulator. set_color uses the terminfo database to look up how to change terminal colors on whatever terminal is in use. Some systems have old and incomplete terminfo databases, and may lack color information for terminals that support it. Download and install the latest version of ncurses and recompile fish against it in order to fix this issue. Version 1.23.1 Sun Jan 8 2012 set_color(1)
All times are GMT -4. The time now is 06:03 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy