If statement in awk with external variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If statement in awk with external variable
# 1  
Old 12-11-2008
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 year

date=$year$month$day

echo "Enter the source IP (x.x.x.x)"
read source

if (ls /archive/$ID$date.gz | awk '{print $1}'); then

echo "Please wait while your request is being processed."

zcat /archive/$ID$date.gz | awk '{FS = "^"} {if($2==$source) {print $1}}' | awk '{print $7}' | sort | uniq -c | sort -n

else
echo "Couldn't find file"
exit
fi;;



What you see when its run

Enter client ID
1234
Enter month (01, 02, 03)
12
Enter day (03, 15)
10
Enter Year (07, 08)
08
Enter the source IP (x.x.x.x)
10.1.5.42
archive/1234081210.gz
Please wait while your request is being processed.

#


No results but there should be...


However if I run the following manually it works fine

zcat /archive/1234081210.gz | awk '{FS = "^"} {if($2=="10.1.5.42") {print $1}}' | awk '{print $7}' | sort | uniq -c | sort -n
2 122:3:0
5 122:7:0
27 3:13416:1



So the problem seems to do with getting the if($2==$source) part working. I'm not sure if this is where the awk -v would come into play but any help would be appreciated.

TIA
# 2  
Old 12-11-2008
search the awk man page look for awk -v option
that will solve your problem
# 3  
Old 12-11-2008
Hammer & Screwdriver variables in awk

Code:
awk -v src=$source '$2==src

and so on with the rest of your commands
Note that I renamed what the script treated as $source to src; to be shorter in awk and to demonstrate that the same names are not required.
# 4  
Old 12-11-2008
Why would doing it like this provide no results?

Code:
zcat /archive/$ID$date.gz | awk -v src=$source '{FS = "^"} {if($2==$src) {print $1}}' | awk '{print $7}' | sort | uniq -c | s$

# 5  
Old 12-11-2008
Hammer & Screwdriver

Not
Code:
{if($2==$src) {print $1}}'

but
Code:
{if($2==src) {print $1}}'

Use the $ in scripts, but not inside awk
# 6  
Old 12-11-2008
whoops cut some of it off in the previous post.

Code:
zcat /archive/$ID$date.gz | awk -v src=$source '{FS = "^"} {if($2==$src) {print $1}}' | awk '{print $7}' | sort | uniq -c | sort -n

# 7  
Old 12-11-2008
Quote:
Originally Posted by joeyg
Not
Code:
{if($2==$src) {print $1}}'

but
Code:
{if($2==src) {print $1}}'

Use the $ in scripts, but not inside awk
oh yea, forgot to remove it from the $source. That fixed it, thanks man!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - saving results of external script to variable.

So, I've been playing with speeding up some analysis we do by using multiple threads of awk (actually, mawk, but code-compatible as far as I use it) on multiple CPU cores. So, I have a big data file and I have several copies of exactly the same processor script, written in mawk. I also have a... (8 Replies)
Discussion started by: treesloth
8 Replies

2. Shell Programming and Scripting

Passing external variable to awk

Hi, I am trying to write a bash script in which I need to pass a external variable to the awk program. I tired using -v but it not accepting the value. Here is my sample code. #!/usr/bin/bash ###################################################################################### ####... (5 Replies)
Discussion started by: jpkumar10
5 Replies

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

4. Shell Programming and Scripting

Help needed with awk external variable

I'm trying to get the universities result data into different file, where the $9 contains unversity field and field7,4 & 5 contains the keys to sort the students by marks. How to use uni variable to match against $9 inside awk? c=0 for uni in `cat /tmp/global_rank| awk -F ',' '{print... (1 Reply)
Discussion started by: InduInduIndu
1 Replies

5. Shell Programming and Scripting

[awk] - how to insert an external variable

I want to incorporate the variable in the for statement as a column of my processed file. In the INCORRECT example below, it is $i which corresponds to the i in my for loop: for i in x86_64 i686; do awk '{ print $1" "$4" "$5" "$i }'awk $file-$i > processed-$i.log doneThanks! (3 Replies)
Discussion started by: graysky
3 Replies

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

7. Shell Programming and Scripting

using an awk internal variable as parameter for an external array

Hello, I am running a bash script under linux which first defines an CA-array like j=0 num1=120.00 num2=10.00 until do CA='echo $num1 + $j*$num2' j=$ done within the later awk section of this same script I want to read data from a file. If the value of the second column is... (3 Replies)
Discussion started by: MotAah
3 Replies

8. Shell Programming and Scripting

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. #!/usr/bin/sh echo "Enter... (3 Replies)
Discussion started by: chinmayadalai
3 Replies

9. Shell Programming and Scripting

Insert external variable in a AWK pattern

Dear all, ¿How can i insert a variable in a AWK pattern? I have almost succeeded in solving a puzzle with AWK but now i want to make a script. Let me explain. cat file.txt | awk 'BEGIN {RS="\\n\\n"} /tux/ { print "\n"$0 }' I know that this command makes right what i want to do, but mi... (8 Replies)
Discussion started by: antuan
8 Replies

10. Shell Programming and Scripting

awk help (external variable)

i need help with an awk question. i am looking to have an external variable be defined outside of awk but used in awk. so if we have fields $1, $2, $3 so on and so forth, i would like to be able to dictate what field is being printed by something like $. so if i had a counter called test, make it 3... (8 Replies)
Discussion started by: pupp
8 Replies
Login or Register to Ask a Question