Counter based on 10 files or less


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Counter based on 10 files or less

there are around 676 files in a directory and I want to batch every 10 and then remaining 7 files as well for a batch pressing job simultaneously.

I'm trying to pick-up every 10 counter'd files -- batch these files and then move on with the next batch.. after resetting the Counters back to 0;

but what if I am left with remaining [1-9] files like for now I have 7 files which are not falling under ==10 rule .

Code:
FILECOUNTER=0;
for filex  in `ls`; 
do
let "FILECOUNTER++" 
FILENAMES="$filex\t$FILENAMES"
if [[  $FILECOUNTER  -eq 10  ]]
then
$BATCH_THE_PROCESS  `echo -e $FILENAMES`
FILECOUNTER=0; 
FILENAMES="";
fi
done


This loop is running fine for files till 670, but I'm missing last 7 files which are not accounted for in the if-else

Can someone please suggest to get a listing for every 10 or less ( if there are less than 10 files remaining)

Regards,
NMR
# 2  
Had you told us what OS and shell you are using, some smart solutions might have been presented by now.
# 3  
Code:
root# uname -a
Linux hippo 3.16.0-8-amd64 #1 SMP Debian 3.16.64-2 (2019-04-01) x86_64 GNU/Linux


Code:
root# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 8 (jessie)"
NAME="Debian GNU/Linux"
VERSION_ID="8"
VERSION="8 (jessie)"
ID=debian
HOME_URL="http://www.debian.org/"
SUPPORT_URL="http://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"


Code:
root# hostnamectl
   Static hostname: hippo
         Icon name: computer-server
           Chassis: server
        Machine ID: ---------------------------------------
           Boot ID: ---------------------------------------
  Operating System: Debian GNU/Linux 8 (jessie)
            Kernel: Linux 3.16.0-8-amd64
      Architecture: x86-64

Code:
root# echo $BASH_VERSION
4.3.30(1)-release

# 4  
With your recent bash, you have "Arithmetic Expansion" at your finger tips. You don't need the ´ls´ "command substitution". Try
Code:
for FN in *; do FL="$FL $FN"; if ! ((++CNT%10)); then echo "executing " $FL; FL=""; fi; done; [ "$FL" ] && echo "executing " $FL
executing  file1 file2 file3 file4 file5 file6 file7 file8 file9 file10
executing  file11 file12 file13 file14 file15 file16 file17 file18 file19 file20
executing  file21 file22 file23 file24 file25 file26 file27

. (Check for residual files added after Jim McNamara's post) Replace the echo with your actual command. You might also want to try e.g.
Code:
ls | paste -sd"         \n" | while read LN; do echo "executing " $LN; done


Last edited by RudiC; 4 Weeks Ago at 08:41 AM.. Reason: Check for residual files added after Jim McNamara's post
This User Gave Thanks to RudiC For This Post:
# 5  
Code:
parms=""
cnt=0
cd /path/to/files
ls | while read fname
do
     parms="$parms $fname"
     cnt=$(($cnt +1))
     if [ $cnt -eq 10 ] ; then
         batch /path/to/myjobname parms
         cnt=0
         parms=""
     fi
done
# one answer to your question - run any amount of leftovers:
[ $cnt -gt 0 ]  &&   batch /path/to/myjobname $parms

Edit: oops Rudi beat me to it....
# 6  
Using shell arrays:
Code:
FL=($(ls))
while (( CNT <= ${#FL[@]})); do echo "executing " ${FL[@]:CNT:10}; ((CNT+=10)); done


Last edited by RudiC; 4 Weeks Ago at 09:18 AM..
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
How to take input from different files for using the counter variable?
Optimus81
Hi All, I have script which call test utility and it takes token input which is kept in a separate file. now instead of taking tokens from single file,will keep 5 input files and script should read tokens from each files. EX : There will 5 token.txt file which will have say around 1000...... Shell Programming and Scripting
1
Shell Programming and Scripting
grouping log files based on counter
manas_ranjan
I have my log file as below 00:18:02 - Nothing normal; Garbage Collection kicked off & running from last 3 min... 00:19:02 - Nothing normal; Garbage Collection kicked off & running from last 4 min... 00:19:02 - Nothing normal; Garbage Collection kicked off & running from last 4 min......... Shell Programming and Scripting
11
Shell Programming and Scripting
File merging based on different counter loop
manas_ranjan
hello, File 1 main Group sub group MIT VAR_1D_DATA_TYPE 23-03-2012 MIT VAR_1D_DATA_TYPE 22-03-2012 MIT VAR_10D_DATA_TYPE 23-03-2012 MIT VAR_10D_DATA_TYPE 22-03-2012 MIT ...... Shell Programming and Scripting
0
Shell Programming and Scripting
sort the files based on timestamp and execute sorted files in order
saidutta123
Hi I have a requirement like below I need to sort the files based on the timestamp in the file name and run them in sorted order and then archive all the files which are one day old to temp directory My files looks like this PGABOLTXML1D_201108121235.xml...... Shell Programming and Scripting
1
Shell Programming and Scripting
Filename from splitting files to have the same filename of the original file with counter value
natalie23
Hi all, I have a list of xml file. I need to split the files to a different files when see the <ko> tag. The list of filename are B20090908.1100-20090908.1200_CDMA=1,NO=2,SITE=3.xml B20090908.1200-20090908.1300_CDMA=1,NO=2,SITE=3.xml B20090908.1300-20090908.1400_CDMA=1,NO=2,SITE=3.xml ...... Shell Programming and Scripting
3
Shell Programming and Scripting