Use script to mkdir based on file's data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use script to mkdir based on file's data
# 1  
Old 02-07-2018
Use script to mkdir based on file's data

I have a file with lines like:

Code:
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
Code:
111 12 7
111 13 8
112 12 9
115 31 3

I tried many different ways but with no results.

Last edited by Don Cragun; 02-07-2018 at 06:35 PM.. Reason: Change COLOR tags to CODE and ICODE tags.
# 2  
Old 02-07-2018
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)?
# 3  
Old 02-07-2018
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:

Code:
 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
Code:

awk ' f1  {n++}{print > "cat"n++"-"n++30".txt"}' input.dat


Last edited by Scrutinizer; 02-07-2018 at 06:53 PM.. Reason: code tags
# 4  
Old 02-07-2018
Code:
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

# 5  
Old 02-07-2018
It's working great!!!! Smilie
I'm very thankful!SmilieSmilieSmilieSmilieSmilieSmilieSmilieSmilieSmilieSmilieSmilieSmilie
# 6  
Old 02-07-2018
Another solution:

Code:
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[i]
   print "EOF"
}
}' stepsize=10 infile | sh


Last edited by Chubler_XL; 02-07-2018 at 08:38 PM..
# 7  
Old 02-08-2018
Pure shell (recent bash):
Code:
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


Last edited by RudiC; 02-08-2018 at 10:27 AM.. Reason: mentioned bash
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

In PErl script: need to read the data one file and generate multiple files based on the data

We have the data looks like below in a log file. I want to generat files based on the string between two hash(#) symbol like below Source: #ext1#test1.tale2 drop #ext1#test11.tale21 drop #ext1#test123.tale21 drop #ext2#test1.tale21 drop #ext2#test12.tale21 drop #ext3#test11.tale21 drop... (5 Replies)
Discussion started by: Sanjeev G
5 Replies

2. Shell Programming and Scripting

Creating bash script to process a data file based on the menu

Hey, im fairly new to unix and Im trying to make this unix project that would display a menu and do the following. MENU =========================== (p, P) Print users info (a, A) Add new user (s, S) Search user (d, D) Delete user (x,X) Exit Enter your choice: Trying to... (3 Replies)
Discussion started by: ultimaxtrd
3 Replies

3. Shell Programming and Scripting

Script for extracting data from csv file based on column values.

Hi all, I am new to shell script.I need your help to write a shell script. I need to write a shell script to extract data from a .csv file where columns are ',' separated. The file has 5 columns having values say column 1,column 2.....column 5 as below along with their valuesm.... (3 Replies)
Discussion started by: Vivekit82
3 Replies

4. UNIX for Dummies Questions & Answers

Shell script to extract data from csv file based on certain conditions

Hi Guys, I am new to shell script.I need your help to write a shell script. I need to write a shell script to extract data from a .csv file where columns are ',' separated. The file has 5 columns having values say column 1,column 2.....column 5 as below along with their valuesm.... (1 Reply)
Discussion started by: Vivekit82
1 Replies

5. Shell Programming and Scripting

Generate tabular data based on a column value from an existing data file

Hi, I have a data file with : 01/28/2012,1,1,98995 01/28/2012,1,2,7195 01/29/2012,1,1,98995 01/29/2012,1,2,7195 01/30/2012,1,1,98896 01/30/2012,1,2,7083 01/31/2012,1,1,98896 01/31/2012,1,2,7083 02/01/2012,1,1,98896 02/01/2012,1,2,7083 02/02/2012,1,1,98899 02/02/2012,1,2,7083 I... (1 Reply)
Discussion started by: himanish
1 Replies

6. Shell Programming and Scripting

awk based script to find the average of all the columns in a data file

Hi All, I need the modification for the below mentioned code (found in one more post https://www.unix.com/shell-programming-scripting/27161-script-generate-average-values.html) to find the average values for all the columns(but for a specific rows) and print the averages side by side. I have... (4 Replies)
Discussion started by: ks_reddy
4 Replies

7. Shell Programming and Scripting

awk based script to print the "mode(statistics term)" for each column in a data file

Hi All, Thanks all for the continued support so far. Today, I need to find the most occurring string/number(also called mode in statistics terminology) for each column in a data file (.csv type). For one column of data(1.txt) like below Sample 1 2 2 3 4 1 1 1 2 I can find the mode... (6 Replies)
Discussion started by: ks_reddy
6 Replies

8. Shell Programming and Scripting

Awk based script to find the median of all individual columns in a data file

Hi All, I have some data like below. Step1,Param1,Param2,Param3 1,2,3,4 2,3,4,5 2,4,5,6 3,0,1,2 3,0,0,0 3,2,1,3 ........ so on Where I need to find the median(arithmetic) of each column from Param1...to..Param3 for each set of Step1 values. (Sort each specific column, if the... (5 Replies)
Discussion started by: ks_reddy
5 Replies

9. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

10. Shell Programming and Scripting

Extract data based on match against one column data from a long list data

My input file: data_5 Ali 422 2.00E-45 102/253 140/253 24 data_3 Abu 202 60.00E-45 12/23 140/23 28 data_1 Ahmad 256 7.00E-45 120/235 140/235 22 data_4 Aman 365 8.00E-45 15/65 140/65 20 data_10 Jones 869 9.00E-45 65/253 140/253 18... (12 Replies)
Discussion started by: patrick87
12 Replies
Login or Register to Ask a Question