Variable is not evaluated in awk statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable is not evaluated in awk statement
# 1  
Old 05-26-2010
Question Variable is not evaluated in awk statement

Dear All,
I have one problem in my script, awk statement as
1. it is not evaluate the second variable $stake but the first one $channel is being done.
2.I want to assign the whole awk statement to a variable actual_code which is not being executed in my script.

Code:
 #!/usr/bin/sh
echo "Enter the input text file:\c"
read input line by line
while IFS= read -r line
do
        sed '1,6d' /home/controller/test/NAVEIIW_PC_OverWrite_to_NAVEIIW_FD_OverWrite_to_NAVEIICW_FD_Bypass_05132010_03_07PM_RUN1.n2.txt > NEW1.txt
echo "Please enter the value of channel:\c"
read channel
echo "$channel"
echo "Please enter the value of stake:\c"
read stake
echo "$stake"
awk '/"$channel"/&&/"$stake"/
 {count++} END{print count}' NEW1.txt
echo "actual code is $actual_code"
done

Output comes like below:
Code:
Please enter the value of channel:1
1
Please enter the value of stake:U
U
838

actual code is

Here the input file is a text file which contains some data of different column and out of which one column contains 1 and other contains U, I need to filter all the lines that contains both 1 and U and the resulting count should be assigned to a variable.

Code:
some contents of the text file is like this

Nielsen Hybrid Decoder, version 2.0 
 
This product incorporates technology covered by one or more 
of the following United States Patents: 
6,272,176; 6,621,881; and 6,807,230. 
 
Nielsen Hybrid Decoder, version 2.0 
 
This product incorporates technology covered by one or more 
of the following United States Patents: 
6,272,176; 6,621,881; and 6,807,230. 
 
Timestamp, File Time, Channel, SID, Level, Stacked, Count, Block 
2632328662,    26,  1,  1125, FD, U,   4,  2466, Thu May 13 15:10:14 2010 
2632328664,    28,  1,  1125, FD, U,   4,  2653, Thu May 13 15:10:16 2010 
2632328668,    32,  1,  1125, FD, S,   7,  3027, Thu May 13 15:10:20 2010 
2632328559,    34,  1,   436, PC, U,   7,  3214, Thu May 13 15:08:31 2010 
2632328672,    36,  1,  1125, FD, S,   5,  3401, Thu May 13 15:10:24 2010 
2632328563,    38,  1,   436, PC, S,   6,  3588, Thu May 13 15:08:35 2010 
2632328676,    40,  1,  1125, FD, U,   4,  3775, Thu May 13 15:10:28 2010 
2632328567,    42,  1,   436, PC, U,   3,  3962, Thu May 13 15:08:39 2010 
2632328680,    44,  1,  1125, FD, S,  10,  4149, Thu May 13 15:10:32 2010 
2632328571,    46,  1,   436, PC, U,   2,  4336, Thu May 13 15:08:43 2010 
2632328684,    48,  1,  1125, FD, U,   9,  4523, Thu May 13 15:10:36 2010 
2632328575,    50,  1,   436, PC, S,  14,  4710, Thu May 13 15:08:47 2010


Last edited by Scott; 05-26-2010 at 04:28 AM.. Reason: Code tags, please...
# 2  
Old 05-26-2010
Welcome to the forum.

You never declared the variable "actual_code".
Btw, Not clear what you want to do with this variable.

regarding "$channel" and "$stake"

try using this:

Code:
awk -v c="$channel" -v s="$stake" '/c/ && /s/ {......}


Please use code tags next time ( select the code and press # in the toolbar).
This User Gave Thanks to clx For This Post:
# 3  
Old 05-26-2010
Hi,

try changing the awk sentence:

Code:
awk  'BEGIN{FS=", "; count=0; } { if( $3=='$channel' && $6=="'$stake'") count++; } END {print count}'  NEW1.txt

# 4  
Old 05-26-2010
#Variable is not evaluated in awk statement

@albertogarcia: Thanks a lot!!! and ur script works properly.
I would like to receive a suggestion from you.
Could u pls advice me how to filter out the time column which could be the $9 and I need to get the lines which will contains when
$3=1,$6=U,$4=1125, $5=FD and the initial time=Thu May 13 15:10:14 2010 and
final time=Thu May 13 15:41:43 2010.

I mean to say the range of time period in which I have to count the lines along with the other constaints.
Thanks in advance!!!

Thanks
Chinmaya

---------- Post updated at 05:39 AM ---------- Previous update was at 05:15 AM ----------

@albertogarcia: Thanks a lot!!! and ur script works properly.
I would like to receive a suggestion from you.
Could u pls advice me how to filter out the time column which could be the $9 and I need to get the lines which will contains when
$3=1,$6=U,$4=1125, $5=FD and the initial time=Thu May 13 15:10:14 2010 and
final time=Thu May 13 15:41:43 2010.

I mean to say the range of time period in which I have to count the lines along with the other constaints.
Thanks in advance!!!

Thanks
Chinmaya


Quote:
Originally Posted by albertogarcia
Hi,

try changing the awk sentence:

Code:
awk  'BEGIN{FS=", "; count=0; } { if( $3=='$channel' && $6=="'$stake'") count++; } END {print count}'  NEW1.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

2. Shell Programming and Scripting

Variable IF statement in awk

I have multiple requirements to run data extracts (with the same basic requriement / resulsts) however the parameters of the extracts change with each request. To help make life easier I am writing a script where the parameters for the specific request are set at the start and then used as needed.... (6 Replies)
Discussion started by: meike
6 Replies

3. Shell Programming and Scripting

Variable on If Statement

Hi, I am tasked to modify soem script and I come accross a line which I dont fully understand. I tried searching online but I couldnt get a good explanation on it. Here it the part of the code: PAY_RT=`cat $TEMPFILE | cut -f2 -d","` if ; then PAY_RT=R fi What is... (3 Replies)
Discussion started by: The One
3 Replies

4. Shell Programming and Scripting

awk - updating variable in if statement

I have another question I am stuck at :wall: I have a text file with two columns, like so... 2 0.0627279 3 0.0794451 4 0.108705 5 0.137739 6 0.190394 7 0.217407 8 0.241764 9 0.344458 10 0.460762 I'd like to go through the file line by line until the value in the second column... (3 Replies)
Discussion started by: origamisven
3 Replies

5. Programming

Evaluated variables in PHP

I'm putting my club's membership database online. I have a Member table in the database with fields such as FirstName, LastName etc. I have a web form with matching input variables Member_FirstName, Member_LastName that's going to be used for inserting a new member into the database, so I'll be... (2 Replies)
Discussion started by: JerryHone
2 Replies

6. Shell Programming and Scripting

Select variable within a if statement

i want to select a variable created and use it in a if statement, but not getting the desired results LINE='device for 0101a01: lpd://172.25.41.111:515' prt=`echo $LINE | awk '{print $3 }' | cut -c 1-7` echo $prt My if statement to select just what i want.. IFS=$":" while read prt... (11 Replies)
Discussion started by: ggoliath
11 Replies

7. Shell Programming and Scripting

Assigning Variable to AWK statement

Hi, The following command runs on in the Korn shell prompt. however i want to output the value of this to a variable. Can anyone provide a solution? echo 'ABC,DEF,"G,HI,J",KLM,"MNi,O"'| awk -F "\"" '{for(i=1;i<=NF;i++){if(i%2)gsub("\,","~^~",$i)}}1' (2 Replies)
Discussion started by: ladarlsan
2 Replies

8. Shell Programming and Scripting

If statement in awk with external variable

So I have a if statement inside an awk to check if $2 of a awk equals a specific IP but the test fails. So here is what I have. # !/bin/sh echo "Enter client ID" read ID echo "Enter month (01, 02, 03)" read month echo "Enter day (03, 15)" read day echo "Enter Year (07, 08)" read... (6 Replies)
Discussion started by: doublejz
6 Replies

9. UNIX for Dummies Questions & Answers

Assigning evaluated expression to a variable

Hello, Could you please let me know what is the problem here.. 28:var1="SERVER_$j" 29:eval $var1=`grep "^DBSERVER=" "$i" |cut -d"=" -f2` i get this error: syntax error at line 29 : `|' unexpected Thanks for your quick response..This is urgent..pls.. Regards Kaushik (5 Replies)
Discussion started by: kaushikraman
5 Replies

10. Shell Programming and Scripting

Using variable in case statement

I want to do this: Ex 1: case $answer in 1|2|3|4|5) echo $answer;; x) break;; *) echo "Invalid selection. Try again.";; esac But I need the part "1|2|3|4|5" to be fetched from a variable, like so: Ex 2: case $answer in $cases) echo $answer;; x) break;; *) echo "Invalid... (2 Replies)
Discussion started by: fialia
2 Replies
Login or Register to Ask a Question