awk loop issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk loop issue
# 1  
Old 12-10-2012
awk loop issue

Hi

I am not able to solve this minor issue even after a lot of trial! Will be thankful if you can help me out. This is part of a awk script and the loop is self-explanatory -

Code:
num_null_key=0			#for counting the number of null keys
num_non_null_key=0		#for counting the number of non null keys
while (key <= last_key)
{
        if ($key == "")
        {
                full_key = full_key " NULL"		#full_key will be concatenated
                if ($num_non_null_key == 0)	#this condition will be matched once
                	num_null_key++		#only when num_non_null_key=0
                printf("%d",num_null_key)
        }
        else
        {
                full_key = full_key " " key		#full_key will be concatenated
                num_non_null_key++		#so in the next loop the inner 'if' of the previous block will be tested
                non_null_key = non_null_key " " key #non_nulll_key will concatenated
                printf("%d",num_non_null_key)
        }
        key++
}

Suppose the complete key set is as follows - "12 " and so full_key should be "12 NULL NULL NULL NULL". But this is not happening. Also values of num_null_key and num_non_null_key are not getting populated correctly.

What is the actual mistake in my code?

Thanks in advance.
# 2  
Old 12-10-2012
Maybe this loop is self explanatory to everyone else, but without seeing a sample of the input you're processing and knowing whether or not something else earlier in your awk script initializes key and last_key, I have no idea what is going on here.

If by Suppose the complete key set is as follows - "12 ", you mean that key has been set to "12 " and last_key has not been set, then the loop will never be executed. If neither key or last_key have been previously set, the loop will be executed exactly once and will print "1" with no newline.

If key and last_key have both been set to numeric values, the output will be a string of (last_key - key + 1) "1" characters with no newline as long as there are at least last_key - 1 + 1 lines in your input file(s) (assuming that this fragment of code is executed once for each input line).
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 12-10-2012
Also, a variable reference should not be prepended with a $-sign unless you are trying to reference a field value..
and what is your desired output?
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue in awk parsing under while loop

Hi I am trying to parse a grep output using awk. It works fine individually and not working under the loop with variable name assigned. cat > file.txt dict=/dictr/abcd/d1/wq:/dictr/abcd/d2/wq:/dictr/abcd/d3/wq: sample tried code Nos=`grep -w "dict" file.txt | awk -F"=" '{print... (10 Replies)
Discussion started by: ananan
10 Replies

2. Shell Programming and Scripting

Issue with for loop

Hi Team, I have for loop in my shell script. Which basically loop through all files in the directory, When some files are in the directory it works just fine. But if there are no files at all..still the for loop try to execute. Please help. Below is the code. #!/bin/ksh echo "Program... (5 Replies)
Discussion started by: bharath561989
5 Replies

3. Shell Programming and Scripting

awk - 2 files comparison without for loop - multi-line issue

Greetings Experts, I need to handle the views created over monthly retention tables for which every new table in YYYYMMDD format, there is equivalent view created and the older table which might be dropped, the view over it has to be re-created over a dummy table so that it doesn't fail.... (2 Replies)
Discussion started by: chill3chee
2 Replies

4. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

5. Shell Programming and Scripting

While Loop issue

Hi, i=0 t5=6000001 while do i=`expr $i + 1` t5=`expr $t5 + 1` echo $t5 done I am able to increment "col3" value but unable to get col1,col2 value. Input: t1=10001 t2=abc t3=ghkc (5 Replies)
Discussion started by: onesuri
5 Replies

6. Shell Programming and Scripting

For loop, awk command issue

limit.csv data -------------- 5600050 38Nhava 400077 27Bomay rate.txt data ------------- 38NhaVA 27BomaY 27Bomay below is my script: for i in `cat limit.csv` do b=`awk '{print $1}' $i` (4 Replies)
Discussion started by: p_satyambabu
4 Replies

7. Shell Programming and Scripting

Variable value substitution issue with awk command issue

Hi All, I am using the below script which has awk command, but it is not returing the expected result. can some pls help me to correct the command. The below script sample.ksh should give the result if the value of last 4 digits in the variable NM matches with the variable value DAT. The... (7 Replies)
Discussion started by: G.K.K
7 Replies

8. Shell Programming and Scripting

While loop with awk issue

Hi folks. I am trying to use a while loop along with awk to get the colums where 2 specific words are found. Here is the output of the command i run where i need to pull the column numbers if specific words are found: State Type Rebal Sector Block AU Total_MB Free_MB ... (3 Replies)
Discussion started by: Stephan
3 Replies

9. Shell Programming and Scripting

Issue with using While loop

Hi, I am trying to move a file from remote server to local server and when the transfer completes successfully i call a script in remote server to remove the file which was successfully transferred. I do this by first getting the list of file in remote server and move the text file to local... (8 Replies)
Discussion started by: funonnet
8 Replies

10. Shell Programming and Scripting

for-while loop issue

sup experts..i had a script which was bugging me..was hoping someone could point out the issue here Input file: space separated 2 columns I wanted to print out the 2 columns after assigning them to variables ( bascially the same output but iterate through line by line ). The code worked... (7 Replies)
Discussion started by: foal_newbie
7 Replies
Login or Register to Ask a Question