In a directory, for each file listed by ls -1 . in the default order which is the lexicographic order, I want to do the following
find the next
nfiles starting from and including the file, putting them into an array namedtrain, and copy them into a subdirectorytmp.find the
n+1-th file, putting it into a variable namedtestperform some operation on
train,test, andtmp. Remove files intmp.
train and test are reused, and supposed to be overwritten for each file listed by ls.
tmp is supposed to be empty right before starting the above steps for each file listed by ls.
The three steps are performed on each file except the last n files listed by ls, because there is no n+1-th file starting from each of them.
I wonder how to implement the above in bash? Thanks.
For example,
suppose n is 2, and the files listed by ls in the current directory are:
a b c d e f for a, I will find a and b and put them into array train and copy them into dir tmp, and find c and put it in variable test. Then perform some operation on train, test and tmp. Finally, empty dir tmp.
for b, I will find b and c, and put them into array train by overwriting it, and copy them into dir tmp, and find d and put it in variable test by overwriting it. Then perform some operation on train, test and tmp. Finally, empty dir tmp.
Then the above is performed on c and d, but not on e and f, because n=2.
ls | tail -n +3 | head -n2gave you the 3rd and 4th files.ls, that's a dirty habit