awk variable problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk variable problem
# 1  
Old 02-09-2011
awk variable problem

Hi

How are you people ..

I am facing another problem regarding awk below is my code
Code:
-bash-3.00$ export a=10
-bash-3.00$ echo $a
10
-bash-3.00$  echo "" | nawk -v var=$a ' { if(var=10) {  print "abc" } else { print "def"}  }  '
abc
-bash-3.00$ echo "" | nawk -v var=$a ' { if(var==10) {  print "abc" } else { print "def"}  }  '
abc


Why in both cases it is printing abc why not def.

Please help

Last edited by Scott; 02-09-2011 at 11:14 AM.. Reason: Code tags, please...
# 2  
Old 02-09-2011
The first one is wrong. It assigns 10 to a in the if statement (= is for assignment, == is for comparison).

The second one is correct, and prints abc because that's what you asked it to do.

Code:
# a=10
# nawk -v var=$a 'BEGIN { if(var==10) { print "abc" } else { print "def"} } '
abc

a is 10, so if a==10, print abc.
# 3  
Old 02-09-2011
Hi Scottn

thanks a lot for ur reply

thanks a lot
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with awk instructions and bash variable

Hi everyone, I'm trying to write a small script to automatize row data treatment. However, I got some trouble with the awk command. I want to use awk to extract a define paragraph from a text file. The first and final lines are defined externally in two variables called debut and fin. I... (2 Replies)
Discussion started by: TeaTimeSF
2 Replies

2. UNIX for Dummies Questions & Answers

Awk: problem for loop through variable

Hi, input: AAA|1 my script (the function is just an example): gawk 'BEGIN{FS=OFS="|"} function repeat(str, n, rep, i){ for(i=1; i<=n; i++) rep=rep str return rep } { variable_1=repeat($1,$2) variable_2=repeat($1,$2+1) variable_3=repeat($1,$2+3) ... (5 Replies)
Discussion started by: beca123456
5 Replies

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

4. Shell Programming and Scripting

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 :wall: #!/bin/bash -xv ... (4 Replies)
Discussion started by: vishwas.s
4 Replies

5. Homework & Coursework Questions

problem with printing out variable in awk

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: couldn't print out stored variable in awk 2. Relevant commands, code, scripts, algorithms: i have in a... (5 Replies)
Discussion started by: ymc1g11
5 Replies

6. Shell Programming and Scripting

Problem using variable inside awk

HI, This is the code I am using: awk -v aaa="connect" 'BEGIN {IGNORECASE} /aaa/,/!/ {print NR}' bb This does not throw any error but it does not work. Pls help Thanks. (4 Replies)
Discussion started by: sudvishw
4 Replies

7. Shell Programming and Scripting

Problem with Variable and AWK command

Okay, so I am trying to use a count variable to reference the column of output sent from an echo statement. So I am trying to do this #!/bin/bash CURRENT=$PWD VAR=3 CHANGE=`echo $CURRENT | awk -F "/" '{ print \$$VAR }'` This instead of giving me the third instance after the "/" gives... (4 Replies)
Discussion started by: mkjp2011
4 Replies

8. Shell Programming and Scripting

awk problem when attributing query to a variable

Hello, I'm trying to make a script which reads the PID of a process using awk. I've read the thread in this forum where it is explained. However, the problem i'm having is when attributing that PID value to a variable. I'm using AIX 5.3. The command is the following: :/home/user1>ps -ef |... (2 Replies)
Discussion started by: mambo
2 Replies

9. Shell Programming and Scripting

Problem with assigning output of grep + awk to a variable

Hi All, I am getting the output for the following command when i run it on the unix console. --------------------------- grep `whoami` /etc/passwd | awk '{print ($1);}' | cut -d ":" -f3 ---------------------------- But i made it into a script and tried to print the variable, its... (5 Replies)
Discussion started by: meheretoknow
5 Replies

10. UNIX Desktop Questions & Answers

problem while storing the output of awk to variable

Hi, i have some files in one directory(say some sample dir) whose names will be like the following. some_file1.txt some_file2.txt. i need to get the last modified file size based on file name pattern like some_ here i am able to get the value of the last modified file size using the... (5 Replies)
Discussion started by: eswarreddya
5 Replies
Login or Register to Ask a Question