Conditional File Selection From a Directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conditional File Selection From a Directory
# 1  
Old 08-30-2013
Conditional File Selection From a Directory

I have a directory with files that was saved all along the day at different times. Every day I have to run a program that will select 50 files for each hour which has the maximum score.

Sample file name: a_b_c_d_e_f
where a,b,c,d,e,f are double values

Score calculation:
(a + b + c + d + e + f)*(size of the file)


Example:
Sample file name: 0.06_0.17_0.11_1.0_1.0_1.0
Size of the file: 2030 bytes
Score = (0.06 + 0.17 + 0.11 + 1.0 + 1.0 + 1.0)*2030

Likewise, it should calculate scores of all the files and select 50 having highest scores for each hour.
# 2  
Old 08-30-2013
Have you tried anything at all? If so, post your code and error messages.

Regards,
Alister
# 3  
Old 09-03-2013
This is how I am calculating the score of each file... splitting the filename and adding the parts and multiply it with the filesize....

Now what I am not able to do is to take files with 50 highest scores for every hour in the day ??

Code:
cd <folder>

for file in *.*
do
  
  # get the filename
  filename=$(stat -r $file | awk -F" " '{print $16}' )
  
  # get the filesize
  filesize=$(stat -r $file | awk -F" " '{print $8}' )
  
  # split the filename and store in an array
  IFS='_' read -a arr <<< "${filename}"
  
  # find the sum of the filename splitted float values
  total=0
  for i in ${arr[@]}; do
    total=`echo $total + $i | bc`;
  done

  # find the score of the file = (filesize * sum of the floats)
  score=$(echo $filesize $total | awk '{printf "%4.3f",$1*$2}')

done

# 4  
Old 09-03-2013
Which os you are using what is the version of stat please?
# 5  
Old 09-03-2013
I m using mac osx. Can't find version of stat.
Sorry..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Support for Unicode in GTK2 and GTK3 file selection box?

I'm on Tiny Core Linux Pure64 10.1. My locale is en_US.UTF-8 and I generally have no trouble with Unicode characters with one exception: When I try to use Unicode characters in GTK applications' file selection box, I get "Invalid file name": http://files.dantas.airpost.net/public/save_file.jpg ... (11 Replies)
Discussion started by: DevuanFan
11 Replies

2. Shell Programming and Scripting

awk to format file with conditional split

In the awk below I am splitting $7 on the : (colon) then - (hyphen) as array a. The word chr is printed at the start of every $1 line. Next, $4 is split on the > (greater then) as array b. I am not sure how to account for the two other possibilities in $4 so the correct output is printed. Every... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

Conditional splitting in more file

Dear All, I was wondering in how split one file in multiple file in a conditional way. Briefly, I have a file like this: >Id1 textA >Id2 textBand my outputs file (2 in this case) shoul be: Id1 >Id1 textAId2 >Id2 textBhope you may help me. Best G (5 Replies)
Discussion started by: giuliangiuseppe
5 Replies

4. UNIX for Dummies Questions & Answers

Random selection of subset of sample from file

Hello Could you please help me to find a code that can randomly select 1224 lines from a file of 12240 and make tn output with 1224 line each. my input is txt file with 12240 lines like : 13474 999003507 0 0 2 -9 13475 999003508 0 0 2 -9 13476 999003509 0 0 1 -9 13477 999003510 0 0 1 -9 ... (7 Replies)
Discussion started by: biopsy
7 Replies

5. Programming

Conditional replace after reading in a file

I need to read the contents of a file. Then I need to grep for a keyword and replace part of the grepped line based on the condition of previous and present line. Example input file: V { port1 = P; port2 = 0; shift_port = P0; /* if next shift_port is P0 I need... (7 Replies)
Discussion started by: naveen@
7 Replies

6. Shell Programming and Scripting

Conditional File Splitting in Unix

Hello Guys, I am new to Unix and i got one requirement where I need to split my file into maximum 5 files on below conditions, if they are splitted into less than 5 then also we are fine. Columns D value should appear in a single file only and shouldn't continue in other files. ... (1 Reply)
Discussion started by: Rizzu155
1 Replies

7. Shell Programming and Scripting

conditional append one line in file.

Hi, Unix gurus, I have a requirement as following: checking existing file, if the file only contain one line. then append "No data" else keep existing file as is. can i achieve this by in command line without write a script. :wall: Thanks in advance. (4 Replies)
Discussion started by: ken002
4 Replies

8. Shell Programming and Scripting

Perform Operations on One File Conditional on Data in Another File

Hello all, I am looking for a solution to the following problem. Perl or python solutions also welcome. Given this input: And this input: I want to get this output. The rule being that if the number in the first file is < 0.9, then the corresponding two columns on... (2 Replies)
Discussion started by: hydrabane
2 Replies

9. Shell Programming and Scripting

Random File Selection and Moving

OK, I am stumpped. I have this shell Script that I want to randomly select a file with the extention of .sct. Then using a portion of its file name select the six related .mot files. Then move them all to another folder. I also need a user input form for the number of .SCT files to randomly select... (6 Replies)
Discussion started by: stak1993
6 Replies

10. Shell Programming and Scripting

Random lines selection form a file.

>cat data.dat 0001 Robbert 0002 Nick 0003 Mark ....... 1000 Jarek (3 Replies)
Discussion started by: McLan
3 Replies
Login or Register to Ask a Question