Home Man
Today's Posts

If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

Array length: ls and sort

ls, sort

Login to Reply

Thread Tools Search this Thread
# 1  
Old 12-14-2017
Array length: ls and sort

Hi there,

I'm listing files and sorting them. When I try to get length of array variable in which these files are stored I get 1 as value. That's weird.


    files_info="$(find $input_dir -name "*_CHR$i.info"  | sort )"

    printf ${#files_info[@]}"\n" #print length 

#--loop through array - it works fine
    for x in ${files_info[@]}
        printf "$x\n"

When I loop through the array it works fine. But when I want to print length of array it print 1.
When I debug it with -x, I see '\n'
May be new line character is messing length?

Please guide.
# 2  
Old 12-14-2017
You're not assigning an array but a scalar variable, thus the result 1 is correct. Unfortunately, you don't mention your environment, so I can't tell you how to define an array.
# 3  
Old 12-14-2017
Linux #1 SMP Debian 4.9.51-1 (2017-09-28) x86_64 GNU/Linux
# 4  
Old 12-14-2017

declare -a myarray=( ` ls command goes here ` )

# 5  
Old 12-14-2017
Never knew about declare. Sorry. When to use it?
# 6  
Old 12-14-2017
For at least bash and ksh arrays, you could replace your code with:
    files_info=( $(find $input_dir -name "*_CHR$i.info"  | sort ) )

    printf ${#files_info[@]}"\n" #print length 

#--loop through array - it works fine
    printf "%s\n" "${file_info[@]}"

as long as none of the pathnames returned by find contain any <space>s or <tab>s.
# 7  
Old 12-14-2017
Thanks don.
this should be good!
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Integer array length yifangt Programming 19 09-09-2013 01:07 PM
How to sort when there is variable length decimal points.? rveri Shell Programming and Scripting 4 07-10-2013 12:02 AM
Sort a hash based on the string length of the values jdilts Shell Programming and Scripting 1 01-16-2013 05:42 PM
Array Length Reports as Having Length when it is Empty? mrm5102 Shell Programming and Scripting 17 09-20-2012 02:41 PM
sort file specifying record length jcagle Shell Programming and Scripting 5 10-11-2011 10:07 AM
Unix sort for fixed length columns and records CheeseHead1 Shell Programming and Scripting 1 11-06-2010 01:52 AM
Need a sort solution for fixed length file mb1201 Shell Programming and Scripting 1 03-02-2009 06:05 PM
how to sort strings by length? rockbike Shell Programming and Scripting 7 02-18-2009 02:48 AM
Array length in PERL anent Shell Programming and Scripting 5 07-17-2008 04:39 PM
sort on fixed length files sach_in Shell Programming and Scripting 6 10-26-2006 09:37 AM

All times are GMT -4. The time now is 08:48 AM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
Show Password