Accessing variable from awk program in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Accessing variable from awk program in shell
# 1  
Old 07-07-2009
Accessing variable from awk program in shell

Hi,
I want to access a variable outside the awk program. My program is as below:- I can not access the exact value of k (See the last line of the program).

#!/usr/bin/sh
j=10
k=1
#k is declared outside awk
awk '
BEGIN {
i=1;
j1="'"$j"'"
printf("\n ## Value of j1 is %d ##", j1);
while (i<=j1)
{
k=i*i;
printf("\n The square of %d is %d", i, k);
i=i+1;
}
printf("\n");
}
'
#Accessing the variable out side the awk program
echo "The value of k is $k"

The current Output is:

## Value of j1 is 10 ##
The square of 1 is 1
The square of 2 is 4
The square of 3 is 9
The square of 4 is 16
The square of 5 is 25
The square of 6 is 36
The square of 7 is 49
The square of 8 is 64
The square of 9 is 81
The square of 10 is 100
The value of k is 1

At the last line, the value of k should be 100...How its possible..?


Second thing is: In the above program, If I delete the line (or comment it)
"printf("\n ## Value of j1 is %d ##", j1);" , then I am getting the following out put:
The square of 1 is 1
The value of k is 1

Question is: Why the output is not comming as before(ie square of first 10 numbers..)...? If I want to get all squares upto 10 without that printf statement, then what should be done..?
# 2  
Old 07-07-2009
I am not clear why you are using awk in your program. you can very much get away without that.
# 3  
Old 07-07-2009
rakeshawasthi, Thanks.
My requirement is not to get the square of numbers. This script is just an example to make understand the exact problem. There are some variables which I am modifying under awk and finally I need to access those from the outside of the awk. This script is a perfect example of my requirement.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with awk and accessing variable values within the syntax

I'm writing a bash script, and I am having trouble with this line: awk '{print "url = \"http://www.example.com/directory/$type/"$1"\""}' input.file > output.file Within the URL being printed, I wish the value of the variable "$type" to be printed, after being read from user input from the... (2 Replies)
Discussion started by: meridionaljet
2 Replies

2. Shell Programming and Scripting

Shell variable to c++ program as input

I have an Shell script which has few global variables eg : range=100; echo "$range" I want to use the same variable in my C++ program for example int main() { cout << range << "\n"; } i tried using this int main(int argc, char *argv) { cout << range << "\n"; } but... (5 Replies)
Discussion started by: shashi792
5 Replies

3. Shell Programming and Scripting

Using Shell variable within awk program

I have a fixed width text file data.txt delimited by pipe. My requirement is to replace the second column values by *. Problem is that when tried access the shell variable in the awk program, value of shell variable is not printed instead only the name of the variable is printed. Please help to... (4 Replies)
Discussion started by: VijayakumarS
4 Replies

4. Shell Programming and Scripting

Accessing shell arrays in awk

I am trying to pass a shell array I created to match on values amongst other things. I have a sample: #! /bin/ksh i=0 for id in `cat id.lst` do IDs=$id (( i=i+1 )) done cat sqloutput.txt | awk -F, -v ids=$ID '{ # Do stuff }' But it looks as if I cannot do this.... (1 Reply)
Discussion started by: Zombywoof
1 Replies

5. Shell Programming and Scripting

problem accessing Multiple Variables from C Program to a Shell script

program name--test #!/bin/bash output1=`/home/user/a.c` output2=`/home/user/a.c` k=`$output1 + 1` m=`$output2 + 1` echo $k echo $m --------------------------------------------------------------------------- prgram name--a.c #include<stdio.h> int main() (1 Reply)
Discussion started by: sameworld1980
1 Replies

6. Shell Programming and Scripting

Accessing awk array from shell

Hi, i want awk to read a file and place it's content into two arrays. When trying to read these arrays with a "for a in ${source_path} "-Loop it gives the right result. But when trying to access directly (/bin/echo ${source_path}) it doesn't work. I read "all "the awk threads in this forum and... (6 Replies)
Discussion started by: bateman23
6 Replies

7. Shell Programming and Scripting

Could someone give me an example of awk accessing array defined in Korn Shell?

As per title and much apprecieated! (2 Replies)
Discussion started by: biglau
2 Replies

8. Shell Programming and Scripting

Accessing Shell Variables in awk or sed

Hello, I wonder if it is possible to pass and use variables from shell environment into sed or awk. I am trying to achieve something similar to the following using sed or awk: var=some_regular_expression grep "$var" filename # Will extract lines from filename The following code,... (3 Replies)
Discussion started by: nasersh
3 Replies

9. UNIX for Dummies Questions & Answers

accessing shell script variable in file

Hi, I have a shell script in which there is a file conn_$temp where $temp has the pid of the shell script. in this shell script i have an embedded awk script that must read the file while ((getline < "conn_$temp") > 0) However due to the "$temp" in the file name, the awk script is... (6 Replies)
Discussion started by: HIMANI
6 Replies

10. Shell Programming and Scripting

AWK program with array variable

Hi, I made a small awk program just to test array variables. I couldn't find anything wrong with it. But it doesn't give out valid numbers.( just 0.00 ) Do you see any problem that I didn't see? Thanks in advance! Here is the program: ################################## BEGIN { FS =... (4 Replies)
Discussion started by: whatisthis
4 Replies
Login or Register to Ask a Question