The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
creating a dynamic array trichyselva Shell Programming and Scripting 1 07-10-2008 06:13 AM
creating array variable scriptingmani Shell Programming and Scripting 2 06-28-2007 07:25 AM
How can i get directory listing? haisubbu UNIX for Dummies Questions & Answers 2 08-25-2006 06:03 AM
creating a dynamic array in ksh gundu Shell Programming and Scripting 3 03-09-2005 11:26 AM
Recursive directory listing without listing files psingh UNIX for Dummies Questions & Answers 4 05-10-2002 07:52 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 4 Weeks Ago
Registered User
 

Join Date: Feb 2008
Posts: 3
Creating a string array from a directory listing

Hi all,

I'd like to create a string array from a long directory listing, extracting only files last modified on a specific date (e.g. Aug 08). I tried the following:

aug8=`ls -ltr |grep 'Aug 08'`

The result was an array (I think) but all of the output from the listing went to the first element of the array, so that:

echo ${aug8[0]}

displayed everything and:

echo ${aug8[1]}

displayed nothing.

Any suggestions as to how I can get each filename in the directory listing to be a single element in the "a8" array?

FYI, I'm in Korn Shell.

THANKS!
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 4 Weeks Ago
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,035
The reason why your command failed was that the shell was not aware that "aug8" should be an array. That you found the output of "ls" in aug8[0] was just because for every variable this is the case:

Code:
willy="x"
print - $willy[0]   # will yield "x"
If you want to assign the content to an array you would have to use the "set -A" subcommand in the shell. Alas, the output of "ls -ltr" contains not only the filenames but also a lot of other information which is why it will have to be trimmed before. I have not Unix-system at hand writing this, but it would be something like the following:

set -A aug8 "$(ls -ltr | sed -n '/Aug 08/ s/ */ /gp' | cut -d' ' -f12)"

There are two problems with this approach anyways: first, while evaluating the commandline the subshell will be executed and the command be replaced by its output. UNIX-commandlines have a maximum length (4096 characters) which could be exceeded if there are enough files with long enough names.

Secondly, ksh-arrays have a maximum number of elements, which is 1024. This might be not enough if the directory contains enough matching files (although presumably the other limit would be hit first).

The first limitation could be circumvented by using a loop to assign the array elements:

Code:
(( iCnt=0 ))
ls -ltr | sed -n '/Aug 08/ s/  */ /gp' | cut -d' ' -f12 | while read filename ; do
     aug8[$iCnt]="$filename"
     (( iCnt += 1 ))
done
But the second limitation cannot be overcome: arrays cannot exceed 1024 elements. Still i cannot understand what it would be necessary to create the array in first place. If you want to process these files one after the other apply the logic from my second example and instead of assigning the content of variable $filename to an array element do some processing with it:

Code:
ls -ltr | sed -n '/Aug 08/ s/  */ /gp' | cut -d' ' -f12 | while read filename ; do
     do_something "$filename"
done
I hope this helps.

bakunin
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




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


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0