[solved] awk: test assoc. array for content


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [solved] awk: test assoc. array for content
# 1  
Old 07-11-2013
[solved] awk: test assoc. array for content

Hi all,

I am looking for a quick/short way in awk to check if an associative array has any content.
I know I can split() it to an indexed array and check if the 1st element is set, or cycle through it with something like for( ele in arr ), but I want to avoid that, as I am looking for a shorter option.

If possible, I don't want to cycle through elements (I do not know what contents will be in there though or what key names will be used) - I would like to straight test.

Any ideas?

Last edited by zaxxon; 07-11-2013 at 03:59 AM.. Reason: rephrased
# 2  
Old 07-11-2013
Considering the number of views of the thread and that I couldn't find anything in the documentations, I assume there is nothing like that. Meanwhile I just increased a hit-counter variable and check if it is set like:
Code:
...
/somepattern/ { hit_cnt[$3]++ }
...
END{
          ## Anything to process?
          for( anything in hit_cnt ){
                    start++
          }

          if( start ){
...

# 3  
Old 07-11-2013
Hello zaxxon,

perl/python provide a function to determine the length of an array. I did a quick search for something similar and found this link.

Quote:
Posix does not define a way to get the length of an array...
.
.
Some awk implementations (gnu awk, true awk) allow you to use length on an array
I'm sure you would have come across these theories in your search... but just a re-iteration for discussion/documentation sake.

In case you stumble upon something better, it would be good to know that too. Thanks.
This User Gave Thanks to balajesuri For This Post:
# 4  
Old 07-11-2013
Hi balajesuri,
thanks for the info! Indeed, length(hit_cnt) works for me since I am using GNU awk.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If test array element multiplication

Ya, I know, who in this day and age is mirroring rootvg...? But yes, my shop does and I need to script checking for it. I also know I could just inverse the the logic and call the LV mirrored if the LPs and PPs were not equal. But I want to do the math in the if test and also know I could... (5 Replies)
Discussion started by: gtsonoma
5 Replies

2. Shell Programming and Scripting

[Solved] If doesn't evaluate the first test

Good afternoon, I am tearing hair out over this. It should be so simple. I have an if statement to evaluate whether or not replication is working. I am testing variables from mysql to see if they are both "Yes". I have put some echo statements in to see how far the code proceeds. It always... (6 Replies)
Discussion started by: jimm
6 Replies

3. Shell Programming and Scripting

How do I test the first char of each line in an array

Hi folks, I am self-learning as I can I have a script that has read a file into an array. I can read out each line in the array with the code: for INDEX in {0..$LENGTH} ## $LENGTH was determined at the read in do echo "${data}" done What I need to do is test the first char... (2 Replies)
Discussion started by: Marc G
2 Replies

4. Shell Programming and Scripting

[Solved] KSH: Array/If Help

RedHat 5 KSH I am creating an array, and then using case to go through and count for specific words. Then the count gets stored as an expression. string='ftp rcp rsh telnet ftp ftp' set -A myarray $string FTPCOUNT="0" for command in ${myarray} do case $command in ftp) FTPCOUNT=`expr... (2 Replies)
Discussion started by: nitrobass24
2 Replies

5. Shell Programming and Scripting

Print @array content to a file

Hi, as the title, I have an array @f_lines with gene information in it. How can I put the content of @f_lines into a file so that I can read it? I tried this: open(OUTPUT, "file"); # put gene information in this file; @f_lines = ("gene1", "gene2", "gene3"...); # gene information; print... (3 Replies)
Discussion started by: lyni2ULF
3 Replies

6. Shell Programming and Scripting

[Solved] Bash test 2 variables to see if ones greater by n

Experts, I have a bash shell script that generates 2 variables that have the current minute and a minute from a log file. Can someone please show me the best way to test if the minutes stray by 5. So basically if: This is ok: Last Fitting Min ============= 02 Current Minute =============... (2 Replies)
Discussion started by: jaysunn
2 Replies

7. UNIX for Dummies Questions & Answers

How to maintain the content of array in any directory I go?

Hi all, I have this scenario where:- The file that I want to save its name into array df is my.08120323.trx which is located in the dir as below: $ pwd /u01/abc/def/SRC_datafiles $ ls *trx my.08120323.trx $ df=*"trx" ##keeping the filename... (1 Reply)
Discussion started by: luna_soleil
1 Replies

8. Shell Programming and Scripting

file content in an array PERL

Hello everybody, I'm new in this forum. I searched a long time for a solution for my problem but I didn't find the right thing. I have to read from a file (content is "abngjm" without any other signs) and have to write this content in an array. But every sign has to be called by its own... (5 Replies)
Discussion started by: e_prof
5 Replies

9. Shell Programming and Scripting

convert variable content to array

Hi All, I have a variable in a shell script which holds let say n paarmeteres with space separate them : $var = par1 par2 par3 par4 parn; so if I print this variable this is what I'll see: par1 par2 par3 par4 parn I need to insert each parameter to an array , so I can go over on each... (3 Replies)
Discussion started by: Alalush
3 Replies
Login or Register to Ask a Question