Parsing Directory Names for Use as Bash Variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing Directory Names for Use as Bash Variables
# 1  
Old 07-24-2008
Parsing Directory Names for Use as Bash Variables

Hi all:
I have a directory where all of the subdirectories are named by the convention "images_#1:#2_Date." My goal is to get an array for each subdirectory that has the structure (#1,#2, int). I am able to use awk to print each subdirectory's values, but cannot figure out how to get them into an array. My script can be seen below:

#! /bin/bash
IFS=""
ls -f|sed 's/_/ /'|sed 's/_/ /'|sed 's/:/ /'|awk '{print $2,$3}'|sort

This gives output of:
501 12
502 14, etc.

I attempted:

#! /bin/bash
cd images
IFS=""
my_array=$(ls -f|sed 's/_/ /'|sed 's/_/ /'|sed 's/:/ /'|awk '{print $2}'|sort)
echo ${my_array[5]}

But this just outputs the entire list with a '[5]' at the end. I am at a loss for how to proceed. Any help you can provide would be greatly appreciated. Thanks.
# 2  
Old 07-24-2008
Bash didn't seem to like passing the variables out of a loop but here's something with ksh.

Code:
#! /bin/ksh
# Get the listing and loop thru to add to the array.
cnt=0
ls -1 *Date |sed -e 's/_/ /g' -e 's/:/ /' | awk '{print $2,$3}'|sort | while read line
do
  if [ ! -z "$line" ]
  then
    myarray[$cnt]="$line"
    cnt=$(($cnt+1))
  fi
done
# print out the array.
ncnt=0
while [ $ncnt -lt $cnt ]
do
  echo "My Array: ${myarray[$ncnt]}"
    ncnt=$(($ncnt+1))
done

Hope it helps
# 3  
Old 07-25-2008
This does not appear to be working. When I ran it as in the post above, I simply got no input. I inserted a second "if" loop to see if, in fact, the lines were being read. They are not. With the current version of the script, seen below, the output is:
>
No length
No length
>

This seems odd, because there are currently 39 directories, and the output of the first command, when run on its own, has 41 lines (the 39 directories plus lines for "./" and "../" Any ideas as to what could be causing this? Thanks again.

A bit of a change from the original intent, right now I am just trying to get an array of the first number in the directory, and I had the format slightly wrong. The directories are actually: images_#1_#2_Date/. I figure if I can get the first number working, getting the second into the array should be trivial.

Quote:
#! /bin/ksh
cd images
cnt=0
ls -f1 | sed -e 's/_/ /g' -e 's/_/ /g'| awk '{print $2}'| sort | while read line

do
if [[ ! -z "$line" ]]
then
myarray[$cnt]="$line"
cnt=$(($cnt+1))
fi

if [[ -z "$line" ]]
then
echo "No length"
fi
done

ncnt=0
while [ $ncnt -lt $cnt ]
do
echo "My Array: ${myarray[$ncnt]}"
ncnt=$(($ncnt+1))
done
Sorry, the indentation does not seem to be working.

Last edited by aefskysa; 07-25-2008 at 06:20 AM.. Reason: Indents
# 4  
Old 07-25-2008
If I'm reading this correctly, your directories would look something like this.
Quote:
images_501:12_Date
images_502:13_Date
If this is true:
Try this:
Code:
#! /bin/ksh
# cd images
cnt=0
find ./ -name \*Date -type d | sed -e 's/_/ /g' -e 's/:/ /g'| awk '{print $2,$3}'| sort | while read line
do
  if [[ ! -z "$line" ]]
  then
    myarray[$cnt]="$line"
    cnt=$(($cnt+1))
  fi

  if [[ -z "$line" ]]
  then
    echo "No length"
  fi
done

ncnt=0
while [ $ncnt -lt $cnt ]
do
  echo "My Array: ${myarray[$ncnt]}"
  ncnt=$(($ncnt+1))
done

if not, AND they all begin with images then substitute the
Quote:
\*Date
with
Quote:
images\*
You really should have something to make sure you only get the directories you want and not the . and .. directories as well. They will show up as No Length.

Hope this helps
# 5  
Old 07-25-2008
Looks like it should work, but I won't be able to check it for at least a few days. Thanks again for the help.
# 6  
Old 07-26-2008
A solution with bash (directories names in the form images_#1_#2_Date/) :
Code:
~/images> ls
array.sh          images_1_3_Date/  images_2_3_Date/  images_3_3_Date/  images_4_3_Date/  images_5_3_Date/    invalid_8_9_Date/
images_1_1_Date/  images_2_1_Date/  images_3_1_Date/  images_4_1_Date/  images_5_1_Date/  images_6_7_8_Date/
images_1_2_Date/  images_2_2_Date/  images_3_2_Date/  images_4_2_Date/  images_5_2_Date/  images_Date/
~/images> cat array.sh
numbers=$(ls -p | awk -F'[_/]' '/^images_.*_Date\/$/ && NF==5 {print $2,$3,$0}')

declare -a Array1=( $(echo "$numbers"|awk '{print $1}') )
declare -a Array2=( $(echo "$numbers"|awk '{print $2}') )

for ((i=0; i<${#Array1[*]}; i++))
do
   echo "subdir images_${Array1[$i]}_${Array2[$i]}_Date"
done
~/images> array.sh
subdir images_1_1_Date
subdir images_1_2_Date
subdir images_1_3_Date
subdir images_2_1_Date
subdir images_2_2_Date
subdir images_2_3_Date
subdir images_3_1_Date
subdir images_3_2_Date
subdir images_3_3_Date
subdir images_4_1_Date
subdir images_4_2_Date
subdir images_4_3_Date
subdir images_5_1_Date
subdir images_5_2_Date
subdir images_5_3_Date
~/images>

Jean-Pierre.
# 7  
Old 07-26-2008
mph: I still can't get yours to work, but thanks a lot for the attempts.

aigles: That works beautifully. Thank you very much.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

2. Shell Programming and Scripting

How to create files with two or more variables in its names?

Hi all, Iam writing a perl script to create many files with variables in their name. i am able to do it, if iam using only one variable. But with two variables the file names are NOT getting generated in the way i want. plz help me out. 1. open(SHW,">divw_unsigned_50_50_$k.reset") or die... (4 Replies)
Discussion started by: twistedpair
4 Replies

3. Shell Programming and Scripting

Searching for file names with variables

Hello everyone We have a problem about searching and copying files with variables. we have variables like $year $jday $date and we want to search the files whose name contain these variables. we tried *$year*$jday*$date or with ? instead of * thank you everyone!!! (4 Replies)
Discussion started by: miriammiriam
4 Replies

4. Shell Programming and Scripting

Specific directory parsing in a directory tree

Hi friends, Hello again :) i got stuck in problem. Is there any way to get a special directory from directory tree? Here is my problm.." Suppose i have one fix directory structure "/abc/xyz/pqr/"(this will be fix).Under this directory structure i have some other directory and... (6 Replies)
Discussion started by: harpal singh
6 Replies

5. Shell Programming and Scripting

Grepping file names, comparing them to a directory of files, and moving them into a new directory

got it figured out :) (1 Reply)
Discussion started by: sHockz
1 Replies

6. UNIX for Dummies Questions & Answers

Loop through directory and extract sub directory names

I am trying to loop through folders and extract the name of the lowest level subfolder I was running the script below, it returns /bb/bin/prd/newyork /bb/bin/prd/london /bb/bin/prd/tokyo I really want newyork london tokyo I couldn't find a standard variable for the lowest level... (1 Reply)
Discussion started by: personalt
1 Replies

7. UNIX for Dummies Questions & Answers

How to display only Owner and directory/sub directory names under particular root

hai, I am new to Unix, I have a requirement to display owner name , directory or sub directory name, who's owner name is not equal to "oasitqtc". (here "oasitqtc" is the owner of the directory or sub directory.) i have a command (below) which will display all folders and sub folders, but i... (6 Replies)
Discussion started by: gagan4599
6 Replies

8. Shell Programming and Scripting

parsing file names and then grouping similar files

Hello Friends, I have .tar files which exists under different directories after the below code is run: find . -name "*" -type f -print | grep .tar > tmp.txt cat tmp.txt ./dir1/subdir1/subdir2/database-db1_28112009.tar ./dir2/subdir3/database-db2_28112009.tar... (2 Replies)
Discussion started by: EAGL€
2 Replies

9. Shell Programming and Scripting

Searching for file names in a directory while ignoring certain file names

Sun Solaris Unix Question Haven't been able to find any solution for this situation. Let's just say the file names listed below exist in a directory. I want the find command to find all files in this directory but at the same time I want to eliminate certain file names or files with certain... (2 Replies)
Discussion started by: 2reperry
2 Replies

10. Shell Programming and Scripting

AWK: Retrieving names of variables passed with -v

I'm an experienced awk user, but this one has me stumped. I have an awk script which is called from a UNIX command line as you'd expect: myscript.awk -v foo=$1 -v bar=$2 filename My question is this: is there a mechanism for determining the names of the -v variables within a script? ... (3 Replies)
Discussion started by: John Mac
3 Replies
Login or Register to Ask a Question