Using nested for loop to iterate over file names


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using nested for loop to iterate over file names
# 1  
Old 01-21-2015
Using nested for loop to iterate over file names

I'm trying to grab a list of file names from a directory, then process those files 5 at a time. In the link below. Instead of using files I'm using the files array which contains 15 strings starting with AAA.

So I'm trying to assign $fileset 5 of the strings at a time to pass to a command. So $fileset looks like this the first pass:

Code:
AAA BBB CCC DDD  EEE

Then the second iteration:

Code:
FFF GGG HHH III JJJ


Code:
files=( AAA BBB CCC DDD EEE FFF GGG HHH III JJJ KKK LLL MMM NNN OOO )

   echo "Total files in array : ${#files[*]}"
   total=${#files[*]}

   for (( i = 0 ; i < $total ; i++ )) do
   fileset=""

      for (( j = $i ; j < 5 ; j++ )) do
         fileset=$fileset${files[$j]}
      done

      echo $fileset
      fileset=""

   done

Moderator's Comments:
Mod Comment Please use CODE tags for sample input and output as well as for code segments.

Last edited by Don Cragun; 01-22-2015 at 04:52 AM.. Reason: Add CODE and ICODE tags.
# 2  
Old 01-22-2015
Your output will be all below each other:

Try to modify this:
Code:
MAX=3
CNT=0
LIST="aaa bbb ccc ddd eee fff ggg hhh iii"

for L in $LIST;do
	if [[ $CNT -lt $MAX ]]
	then	printf "$L "
	else	printf "\n$L "
		CNT=0
	fi
	CNT=$(( $CNT + 1 ))
done
printf "\n"

Outputs as:
Code:
sh ~/blubber.sh 

aaa bbb ccc 
ddd eee fff 
ggg hhh iii

gl & hth
# 3  
Old 01-22-2015
Telling us what OS and shell you're using helps us create a solution that will work in your environment.

With a 1993 or later version of ksh or with a recent bash, here are a couple of ways to do what you seem to want:
Code:
#!/bin/ksh
files=( AAA BBB CCC DDD EEE FFF GGG HHH III JJJ KKK LLL MMM NNN OOO PPP QQQ)

total=${#files[@]}
echo "Total files in array : $total"

echo 'Using nested for loops...'
fileset=""
for (( i = 0 ; i < total ; i += 5 ))
do	for ((j = 0; j < 5 && (i + j) < total; j++))
	do	fileset="$fileset ${files[i + j]}"
	done
	echo $fileset
	fileset=""
done

echo 'Using array subsets...'
for ((i = 0; i < total; i += 5))
do	fileset="${files[@]:i:5}"
	echo "$fileset"
done

which (with both of those shells) produces the output:
Code:
Total files in array : 17
Using nested for loops...
AAA BBB CCC DDD EEE
FFF GGG HHH III JJJ
KKK LLL MMM NNN OOO
PPP QQQ
Using array subsets...
AAA BBB CCC DDD EEE
FFF GGG HHH III JJJ
KKK LLL MMM NNN OOO
PPP QQQ

# 4  
Old 01-22-2015
Try also (using Don Cragun's array):
Code:
echo "${files[@]}" | xargs -n5 | while read -a ARR; do echo ${#ARR[@]}, ${ARR[@]}; done
5, AAA BBB CCC DDD EEE
5, FFF GGG HHH III JJJ
5, KKK LLL MMM NNN OOO
2, PPP QQQ

# 5  
Old 01-22-2015
Quote:
Originally Posted by RudiC
Try also (using Don Cragun's array):
Code:
echo "${files[@]}" | xargs -n5 | while read -a ARR; do echo ${#ARR[@]}, ${ARR[@]}; done
5, AAA BBB CCC DDD EEE
5, FFF GGG HHH III JJJ
5, KKK LLL MMM NNN OOO
2, PPP QQQ

This works for bash, but not for ksh. With ksh you'd need to use:
Code:
read -A ARR

instead of:
Code:
read -a ARR

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

FORTRAN: Loop over variable file names

Hi guys I'm a beginner in fortran. So excuse me for my naivety, let me briefly describe what I was trying to do. I have let's say 2 files named reac-1 and reac-2. After opening these files I've to do some calculations, close these files and open the same files again in a loop. So my faulty code... (6 Replies)
Discussion started by: saleheen
6 Replies

2. Shell Programming and Scripting

While loop a file containing list of file names until the files are found?

Hi, I have a control file which will contain all filenames(300) files. Loop through all the file names in the control files and check the existence of this file in another directory(same server). I need to infinitely(2 hrs) run this while loop until all the files are found. Once a file is found,... (5 Replies)
Discussion started by: laknar
5 Replies

3. Shell Programming and Scripting

Print file names in a loop

OS : RHEL 6.1 Shell : Bash I have lots of files in /tmp/stage directory as show below. Using a loop, I need to print all the filenames in this directory except those ending with a number. How can I do this ? # pwd /tmp/stage # # # ls -l * -rw-r--r--. 1 root root 0 Oct 7 18:38 stmt1... (2 Replies)
Discussion started by: kraljic
2 Replies

4. Shell Programming and Scripting

Iterate array using loop over ssh

A simple script: #!/bin/bash test=test test1=(test1 test2 test3) echo ${test1 } ssh server 'echo '$test'; echo '${test1 }' ; echo '${test1}' ; for m in $(seq 1 $(echo '${test1 }' | tr " " "\n" | wc -l)); do echo $m ; echo '${test1}'; done'Here is the result: test1 test2 test3 testing... (5 Replies)
Discussion started by: mharald
5 Replies

5. Shell Programming and Scripting

Do loop doesn't iterate

I'm trying to send the file list as parameter to another job and execute it. But the loop doesn't work, the inner job is running only once and not twice as expected for filelist in $(ls -rt *.txt | tail -2) do echo $filelist export filelist cmd="$Program -config $configfile -autoexec... (11 Replies)
Discussion started by: asandy1234
11 Replies

6. UNIX for Dummies Questions & Answers

Iterate/Loop Through XML Node List

I need to load an XML file and loop through a list of nodes in it to execute a shell script for each one using the attributes for each node as parameters for the script. Any ideas? Any help will be much appreciated. (1 Reply)
Discussion started by: bradlecat
1 Replies

7. Shell Programming and Scripting

How to loop(Iterate) through List with foreach(csh)

Hey all,, I know cshell is harmful:) but I am using this just "to know" - for educational purposes!... not for a long-term use. lets say i have a list.. set arr=(x y z e f) I wanna iterate the list with foreach ,, not with while.!! foreach i $arr echo $i end does not work (2 Replies)
Discussion started by: eawedat
2 Replies

8. Shell Programming and Scripting

big xml file with nested loop parse

I have an xml file with the structure: <tag1> <value1>xyx</value1> <value2>123</value2> </tag1> <tag1> <value1>568</value1> <value2>zzzzz</value2> </tag1> where I want to parse each data pair in the this single file, so something like: find first tag1 data pair... (1 Reply)
Discussion started by: unclecameron
1 Replies

9. Shell Programming and Scripting

Problem with File Names under tcsh loop

Hello, I have a question regarding file naming under a loop in tcsh. I have the following code: #!/bin/tcsh foreach file (test/ProteinDirectory/*) # The * is a bunch of ProteinFile1, ProteinFile2, ProteinFile3, etc. sh /bioinfo/home/dgendoo/THREADER/pGenThreader.sh $file $file ... (4 Replies)
Discussion started by: InfoSeeker
4 Replies

10. Shell Programming and Scripting

File Names in a Variable in a loop

Hi All , I am having confusion in a shell script. Please guide me. I need to get multiple files (number of files vary time to time, file names are separated by '|') using FTP get from the remote server. Actually, i call the FTP function in a loop. At the last step, i need to move all the get... (3 Replies)
Discussion started by: spkandy
3 Replies
Login or Register to Ask a Question