Use script to mkdir based on file's data

I have a file with lines like:

111 12 7
111 13 8
112 12 9
115 31 3
120 31 9
123 10 7
125 12

I want to make a script which, split the first column into parts (101-110, 111-120...), and make directories for its part with name (101-110, 111-120...) Also i want in every directory include a file with all data from every part.

Example: directory name ( 111-115 ) --> file name ( f_111-115 ) --> fle data

111 12 7
111 13 8
112 12 9
115 31 3

I tried many different ways but with no results.

Welcome to the forum!

Please become accustomed to provide decent context info of your problem.

It is always helpful to carefully and detailedly phrase a request, and to support it with system info like OS and shell, related environment (variables, options), preferred tools, adequate (representative) sample input and desired output data and the logics connecting the two including your own attempts at a solution, and, if existent, system (error) messages verbatim, to avoid ambiguities and keep people from guessing.

Please show your attempts for analysis and discussion.
And, be consistent when phrasing your request. Do you want steps of 10 for your bin size (as in the written spec), or steps of 5 (as in your example)?

I'm sorry i'm beginner in unix. I want to make a script in vi. My step is a variable $n given by the user.
I didn't post my attempts because they working totally wrong (probably i use wrong commands).

For directories i tried this:

 arr=($(cut -d ' ' -f1 < input.dat))
 mkdir $(for ((i=0;i<${#arr[@]};i=i+$n)); do echo ${arr[$i]-$arr[$i+$n]}; done)

and for files

awk ' f1  {n++}{print > "cat"n++"-"n++30".txt"}' input.dat
awk '
{line[NR]=$0; number[NR]=$1;}
END {
   loop_begin=int((number[1]+10)/10)*10;
   loop_end=int((number[NR]+10)/10)*10;
   line_pointer=1;
   for (i=loop_begin; i<=loop_end; i+=step_size) {
      dir1=number[line_pointer];
      line_count=0;
      while (number[line_pointer] && number[line_pointer] <= i) {
         file_lines[line_count++]=line[line_pointer];
         line_pointer++;
      }
      dir2=number[line_pointer-1];
      directory=dir1 "-" dir2;
      file=directory "/" "f_" directory;
      print "[[ -d " directory " ]] && rm -rf " directory;
      print "mkdir " directory;
      for (j=0; j<line_count; j++) print "echo " file_lines[j] " >> " file;
   }

}
' step_size=10 input_file | sh

It's working great!!!! :b:
I'm very thankful!:):):):):):):):):):):):slight_smile:

Another solution:

awk '{ N[int($1/stepsize)]=N[int($1/stepsize)]"\n"$0 }
END { for(i in N) {
   dir=sprintf("%d-%d",i*stepsize,(i+1)*stepsize-1)
   printf "[ -d %s ] || mkdir %s\n", dir, dir
   printf "cat > %s/f_%s <<EOF", dir, dir
   print N
   print "EOF"
}
}' stepsize=10 infile | sh

Pure shell (recent bash ):

STEP=5
while read LINE
   do   read A B C <<< $LINE
        TM1=$(( (A-1) / STEP * STEP + 1 ))
        TMP=$TM1-$(( TM1 + STEP - 1 ))
        mkdir -p $TMP
        echo $LINE >> $TMP/f-$TMP
   done < file

I really thank all of you!
You helped me so much to understand the logic of how script works in this case.

:):b: