Awk: problem for loop through variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Awk: problem for loop through variable
# 1  
Old 07-03-2015
Awk: problem for loop through variable

Hi,

input:
Code:
AAA|1

my script (the function is just an example):
Code:
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)

   for(j=1; j<=3; j++){
      print variable_j
}
}' input

It returns 3 blank records.

I don't understand why the for loop doesn't want to go through the variable in order to print the value of:
variable_1
variable_2
variable_3
# 2  
Old 07-03-2015
I don't think awk supports indirect referencing for variables.

In this case it looks like you could just use an array anyway.
This User Gave Thanks to CarloM For This Post:
# 3  
Old 07-03-2015
Hi CarloM,

I tried:
Code:
gawk 'BEGIN{FS=OFS="|"}

function repeat(str, n, rep, i){
    for(i=1; i<=n; i++)
        rep=rep str
        return rep
}
{
a[variable_j]=repeat($1, $2+(j-1))

for(k in a){
    for(j=1; j<=3; j++){
        print a[k]
}
}
}' input

but still get blank records...
# 4  
Old 07-03-2015
No surprise. Some comments:

Code:
awk 'BEGIN{FS=OFS="|"} 

function repeat(str, n, rep, i){                # four parameters 
    for(i=1; i<=n; i++)                         # if n is 0, do nothing
        rep=rep str
        return rep 
}
{
a[variable_j]=repeat($1, $2+(j-1))              # variable_j is undefined thus the empty string is used as the index
                                                # repeat called once with TWO parms only! 
                                                # j is undefined, $2 is 1, so 1 + 0 - 1 = 0, so n in repeat is 0 --> 0 loop executions

for(k in a){                                    # k will be the empty string and the ONLY a element.
    for(j=1; j<=3; j++){                        # 
        print a[k]                              # for this single element, print three empty lines
}
}
}' input

This User Gave Thanks to RudiC For This Post:
# 5  
Old 07-03-2015
Quote:
Originally Posted by beca123456
Hi CarloM,

I tried:
Code:
gawk 'BEGIN{FS=OFS="|"}

function repeat(str, n, rep, i){
    for(i=1; i<=n; i++)
        rep=rep str
        return rep
}
{

# when awk reads first record 
# $2 = 1, j is undefined or not set yet and -1
# which is nothing but $2+(j-1) = 0
# so loop inside function repeat will not work
# ultimately function returns rep which is undefined or not set yet
# so a[""] = ""
# since variable_j is not defined or not set
a[variable_j]=repeat($1, $2+(j-1))  

for(k in a){
    # iterate 3 times since contents inside array a is just ""  you received blank output
    for(j=1; j<=3; j++){
        print a[k]
}
}
}' input

but still get blank records...
Here is test

Code:
[akshay@localhost tmp]$ echo "AAA|1" | gawk 'BEGIN{FS=OFS="|"}
function repeat(str, n, rep, i){ 
print "n : "n
    for(i=1; i<=n; i++)
        rep=rep str
        return rep
}
{

a[variable_j]=repeat($1, $2+(j-1))  

for(k in a){
    for(j=1; j<=3; j++){
        print k,a[k]
}
}

}' 
n : 0
|
|
|

This User Gave Thanks to Akshay Hegde For This Post:
# 6  
Old 07-03-2015
Thanks guys for your explanations !

Got it !
Code:
gawk 'BEGIN{FS=OFS="|"}

function repeat(str, n, rep, i){
    for(i=1; i<=n; i++)
        rep=rep str
        return rep
}
{
    for(j=1; j<=3; j++){
        a[j]=repeat($1, $2+(j-1))
        print a[j]
}
}'

output
Code:
AAA
AAAAAA
AAAAAAAAA

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Variable and awk inside for loop

Thanks all for taking time out and reading this thread and big Thanks to all who have come forward for rescue. Background: I have a variable "nbrofcols" that has number of columns from a data file. Now, using this count in for loop, I am trying to get the maximum length of each column present... (7 Replies)
Discussion started by: svks1985
7 Replies

2. Shell Programming and Scripting

awk programming -Passing variable to awk for loop

Hi All, I am new to AWK programming. I have the following for loop in my awk program. cat printhtml.awk: BEGIN -------- <some code here> END{ ----------<some code here> for(N=0; N<H; N++) { for(M=5; M<D; M++) print "\t" D ""; } ----- } ... (2 Replies)
Discussion started by: ctrld
2 Replies

3. Shell Programming and Scripting

String variable concatenation through loop problem

Hi Team!! Please can anyone tell me why the following line does not work properly? str3+=$str2 it seems that str3 variable does not keep its value in order to be concatenated in the next iteration! Thus when i print the result of the line above it returns the str2 value What i want to do is to... (8 Replies)
Discussion started by: paladinaeon
8 Replies

4. Shell Programming and Scripting

awk, double variable, for loop and rsh

Hello folks, I've a (perhaps) simple question. In a text file I've : server_name1: directory1 server_name2: directory2 server_name3: directory3 I want to make a loop that lets me connect and operate on every server: rsh server_name1 "ls -l directory1" I've tried with awk,... (6 Replies)
Discussion started by: gogol_bordello
6 Replies

5. Shell Programming and Scripting

Bourne Shell - Problem with while loop variable scope.

Hello I am having issues with a script I'm working on developing on a Solaris machine. The script is intended to find out how many times a particular user (by given userid) has logged into the local system for more than one hour today. Here is my while loop: last $user | grep -v 'sshd'... (7 Replies)
Discussion started by: DaveRich
7 Replies

6. Shell Programming and Scripting

problem with for loop and a variable with spaces...Hi

Hi there, I don't understand the following behavior: toto:~$ for word in un "deux trois"; do echo $word; done un deux trois toto:~$ sentence='un "deux trois"' toto:~$ for word in $sentence; do echo $word; done un "deux trois" toto:~$ sentence="un 'deux trois'" toto:~$ for word in... (10 Replies)
Discussion started by: chebarbudo
10 Replies

7. Shell Programming and Scripting

using awk in a for loop (getting ksh variable)

Hi, I've tried searching the forums for a case similar to mine but was unsuccessful. This is my first time to use awk so any help would be really appreciated :) I have one file containing data for with the first value in each row being a State Name. I would need to create a separate file... (1 Reply)
Discussion started by: karver
1 Replies

8. Shell Programming and Scripting

Passing a variable to awk while in a shell for loop

I am a newbie to awk and c programming, however am not a unix newbie. However, I do need help with a kshell script I am writing. It is almost complete, the last step is killing me. Any help would be greatly appreciated. What I am trying to do is cat a text file that has usernames. Then, using... (2 Replies)
Discussion started by: synergy_texas
2 Replies

9. Shell Programming and Scripting

Variable problem in for loop with if statement

Hi, Again a little problem. Do not understand good why an empty string is not detected. Here is the program: #!/bin/ksh APR=`date | grep Apr | awk '{print $2$3}'` MAY=`date | grep May | awk '{print $2$3}'` JUN=`date | grep Jun | awk '{print $2$3}'` echo "Variable Apr has value:... (6 Replies)
Discussion started by: ejdv
6 Replies

10. Shell Programming and Scripting

Problem with variable expension using for loop.

Hi, I have a problem with expending variables when used in a for loop: #!/bin/ksh VAR1=aaa VAR2=bbb VAR3=ccc for ITEM in VAR1 VAR2 VAR3 do echo "${ITEM}" done This gives: VAR1 VAR2 VAR3 (2 Replies)
Discussion started by: ejdv
2 Replies
Login or Register to Ask a Question