Unix/Linux Go Back    


UNIX for Beginners Questions & Answers 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

UNIX for Beginners Questions & Answers


Tags
ls, sort

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-14-2017   -   Original Discussion by genome
genome's Unix or Linux Image
genome genome is offline
Registered User
 
Join Date: Nov 2017
Last Activity: 17 January 2018, 11:35 AM EST
Posts: 51
Thanks: 1
Thanked 1 Time in 1 Post
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.



Code:

    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[@]}
    do
        printf "$x\n"
    done

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.
Sponsored Links
    #2  
Old Unix and Linux 12-14-2017   -   Original Discussion by genome
RudiC's Unix or Linux Image
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 19 January 2018, 4:32 AM EST
Location: Aachen, Germany
Posts: 11,954
Thanks: 353
Thanked 3,686 Times in 3,384 Posts
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.
Sponsored Links
    #3  
Old Unix and Linux 12-14-2017   -   Original Discussion by genome
genome's Unix or Linux Image
genome genome is offline
Registered User
 
Join Date: Nov 2017
Last Activity: 17 January 2018, 11:35 AM EST
Posts: 51
Thanks: 1
Thanked 1 Time in 1 Post
Linux #1 SMP Debian 4.9.51-1 (2017-09-28) x86_64 GNU/Linux
    #4  
Old Unix and Linux 12-14-2017   -   Original Discussion by genome
jim mcnamara's Unix or Linux Image
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 15 January 2018, 9:34 AM EST
Location: NM
Posts: 11,284
Thanks: 581
Thanked 1,127 Times in 1,038 Posts
bash:



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

Sponsored Links
    #5  
Old Unix and Linux 12-14-2017   -   Original Discussion by genome
genome's Unix or Linux Image
genome genome is offline
Registered User
 
Join Date: Nov 2017
Last Activity: 17 January 2018, 11:35 AM EST
Posts: 51
Thanks: 1
Thanked 1 Time in 1 Post
Never knew about declare. Sorry. When to use it?
Sponsored Links
    #6  
Old Unix and Linux 12-14-2017   -   Original Discussion by genome
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 19 January 2018, 4:40 AM EST
Location: San Jose, CA, USA
Posts: 10,916
Thanks: 609
Thanked 3,817 Times in 3,261 Posts
For at least bash and ksh arrays, you could replace your code with:


Code:
    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.
Sponsored Links
    #7  
Old Unix and Linux 12-14-2017   -   Original Discussion by genome
genome's Unix or Linux Image
genome genome is offline
Registered User
 
Join Date: Nov 2017
Last Activity: 17 January 2018, 11:35 AM EST
Posts: 51
Thanks: 1
Thanked 1 Time in 1 Post
Thanks don.
($(
this should be good!
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Array Length Reports as Having Length when it is Empty? mrm5102 Shell Programming and Scripting 17 09-20-2012 03:41 PM
sort file specifying record length jcagle Shell Programming and Scripting 5 10-11-2011 11:07 AM
Need a sort solution for fixed length file mb1201 Shell Programming and Scripting 1 03-02-2009 07:05 PM
how to sort strings by length? rockbike Shell Programming and Scripting 7 02-18-2009 03:48 AM
sort on fixed length files sach_in Shell Programming and Scripting 6 10-26-2006 10:37 AM



All times are GMT -4. The time now is 05:42 AM.