Number of elements in Word list


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Number of elements in Word list
# 1  
Old 08-21-2008
Number of elements in Word list

Hello everyone,
can anyone let me know if there is a way to get the count of elements in a word list that I use for a for loop in the way:
for single_result in $results ; do .......
I know I can increment a counter in my for loop, but would there be a way to know the total number of elements in the list without even starting the loop ?
Hope you can help.
Thanks.
# 2  
Old 08-21-2008
Try using awk:
Code:
cnt_elements=$(awk '{print NF}' <<< "$results")

# 3  
Old 08-25-2008
use wc.
wc somefile
# 4  
Old 08-30-2008
I do not seem to be able to use this statement, I am on AIX with ksh88, I don't know about the usability of the <<<. Can you help with more advice?
I did not know you can send a string in input to awk, I thought awk only acts against files, please let me know.
cnt_elements=$(awk '{print NF}' <<< "$results")
# 5  
Old 08-30-2008
@broli suggested wc
Code:
echo $results | wc -c

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

List files with number to select based on number

Hi experts, I am using KSH and I am need to display file with number in front of file names and user can select it by entering the number. I am trying to use following command to display list with numbers. but I do not know how to capture number and identify what file it is to be used for... (5 Replies)
Discussion started by: mysocks
5 Replies

2. Shell Programming and Scripting

Find a word and increment the number in the word & save into new files

Hi All, I am looking for a perl/awk/sed command to auto-increment the numbers line in file, P1.tcl: run_build_model sparc_ifu_dec run_drc set_faults -model path_delay -atpg_effectiveness -fault_coverage add_delay_paths P1 set_atpg -abort_limit 1000 run_atpg -ndetects 1000 I would like... (6 Replies)
Discussion started by: jypark22
6 Replies

3. Shell Programming and Scripting

Issue with the incorrect number of array elements

Hello , I have a file : RestartSession.txt with the below contents : Backup p203pcrw01_OS_Weekly Failed full 10/11/2015 10:00:07 PM 1444572007 10/11/2015 10:26:23 PM 1444573583 0:00 0:26 18.76 1 08 0 0 0 2 2 180668 ... (4 Replies)
Discussion started by: rahul2662
4 Replies

4. Shell Programming and Scripting

Help with sort word followed by exponential number and numeric number at the same time

Input file: ID_34 2E-69 2324 ID_1 0E0 3254 ID_1 0E0 5434 ID_5 0E0 436 ID_1 1E-14 2524 ID_1 5E-52 46437 ID_3 65E-20 45467 ID_1 0E0 6578 ... Desired output file: ID_1 0E0 6578 ID_1 0E0 5434 ID_1 0E0 3254 ID_1 5E-52 46437 ID_1 1E-14 2524 ID_3 65E-20 45467 (5 Replies)
Discussion started by: cpp_beginner
5 Replies

5. Shell Programming and Scripting

Number of elements, average value, min & max from a list of numbers using awk

Hi all, I have a list of numbers. I need an awk command to find out the numbers of elements (number of numbers, sort to speak), the average value the min and max value. Reading the list only once, with awk. Any ideas? Thanks! (5 Replies)
Discussion started by: black_fender
5 Replies

6. Shell Programming and Scripting

Get number of elements in a list

Hi all I would like to know the number of elements in a list. $list=`ls xyz*` I want to get the number of files xyz* in the folder. Anybody please help!!! (5 Replies)
Discussion started by: VidyaVenugopal
5 Replies

7. UNIX for Dummies Questions & Answers

make ls retrive ordered list of elements

Hello friends!! I have a question regarding the use of ls in unix. I have a folder with files: t1.txt t2.txt t3.txt t4.txt ... t10.txt When I make an ls I always get: t10.txt t1.txt t2.txt t3.txt .. t9.txt (2 Replies)
Discussion started by: SaktiPhoenix
2 Replies

8. Shell Programming and Scripting

eval, array, number of elements - couldn't get it

I try to get a number of elements in an array, using dynamic array name. I need the array name to be dynamic. The array name is constructed as 'inf_ln_$nmb', where $nmb is a file line number So, say I have the arr 'inf_ln_4': > for (( el=0; el<${#inf_ln_4}; el++ )); do > echo "$el:... (1 Reply)
Discussion started by: alex_5161
1 Replies

9. Shell Programming and Scripting

How to extract elements in a field using a number

Hi, I face difficulty where the number which I grep before I would like to use it as number to grep again in another file. For example in file 1, I extract the second field and assign to variable "char" in a while loop. And then, I grep again this char to get i and j. char=`echo "${LINE}"|... (17 Replies)
Discussion started by: ahjiefreak
17 Replies

10. AIX

How to list mirrored elements? (pv, vg, lv)

Hello, How can I list mirrored elements, such as PV, VG or LV? (and others if applicable). On AIX 4.3.3 and 5.3.0. I tried the commands: lscfg|grep hd lspv lsvg lsvg -l (of each vg) lslv (and some options) But could not find what am I looking for. I saw a "number of copies", but I am... (4 Replies)
Discussion started by: cactux
4 Replies
Login or Register to Ask a Question