1

The directory contains x files. I get a list of files. I want to split this list into a larger number of n lists, which would have a limited number of elements.

Examples:

files=$( ls -d /*.csv | sort )
echo $files

/100347_111111.csv
/111301_111111.csv
/111301_222222.csv
/256467_111111.csv
/256467_222222.csv
/256467_333333.csv
/256467_444444.csv
/256467_555555.csv
/256467_666666.csv
/256467_777777.csv

From the resulting list I want to create 3 lists. The lists must not have more than 4 elements. The first list should be composed of the first 4 elements from the files, the other list should contain the following 4 elements, the third list should contain the remaining elements.

n1
/100347_111111.csv
/111301_111111.csv
/111301_222222.csv
/256467_111111.csv

n2
/256467_222222.csv
/256467_333333.csv
/256467_444444.csv
/256467_555555.csv

n3
/256467_666666.csv
/256467_777777.csv

Does someone can help, how to create lists as described above?

5
  • I do not know where to start. I think the solution is by using for loop. But I do not really know where to start, I'm an absolute beginner. Commented Nov 6, 2018 at 10:35
  • @Amessihel. do you honestly think OP is thinking split or file? Commented Nov 6, 2018 at 10:41
  • I do not want to split rows in the file. I want the file name list to be subdivided into sublists. Commented Nov 6, 2018 at 10:43
  • 1
    there is no need for lists, lists are almost never used in shell. what do you want to do with lists? Commented Nov 6, 2018 at 10:44
  • @perreal: maybe not. It's an example (you solved it this way). Another search would be with those keywords: bash for loop number Commented Nov 6, 2018 at 10:53

3 Answers 3

1
FILES=( `ls -d * | sort`)
echo "${FILES[@]:0:4}"

Loop of 4

count=4
for i in $(seq 0 $(( ${#FILES[@]}/$count - 1 ))) ;
   do  
     echo "######## Set" $i "#######"; 
     echo "${FILES[@]:$(( i * $count )):$count }" ; 
   done
Sign up to request clarification or add additional context in comments.

1 Comment

@amessihel modified
1

An example which may be reinventing the wheel:

\ls -1 |
    {
        n=0
        cr=""
        pack=1
        while read -r l
        do
            mod=$(($n % 4))
            if [[ "$mod" == "0" ]]
            then
                echo -e "$cr"n"$pack:"
            fi
            echo $l
            n=$((n + 1))
            pack=$((pack + 1))
            cr="\n";
        done
    }

Here, we use the modulo operator to check if a new pack is about to be displayed (n modulo 4 = 0 if n is a multiple of 4).

I used curly brackets {} to put var initialization and the while loop in the same environment (other wise while won't be able to retrieve n, pack and cr variables).

Comments

0

Try split:

ls -d /*.csv | sort | split -l 4 -d

this will create files x01 x02... containing maximum 4 lines.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.