Count the number of subset of files in a directory | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Count the number of subset of files in a directory

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-18-2013
piynik piynik is offline
Registered User
 
Join Date: Jun 2012
Last Activity: 26 February 2013, 3:52 AM EST
Posts: 55
Thanks: 4
Thanked 0 Times in 0 Posts
Count the number of subset of files in a directory

hi I am trying to write a script to count the number of files, with slightly different subset name, in a directory

for example, in directory /data, there are a subset of files that are name as follow


Code:
 
/data/data_1_(1to however many).txt
/data/data_2_(1 to however many).txt
/data/data_3_(1to however many).txt
etc etc etc

now I want to write a script to count the number of files with each subset name (1 to 3 or however many there are)

Thanks for your help

Last edited by Scott; 01-18-2013 at 06:53 AM.. Reason: Code tags, please...
Sponsored Links
    #2  
Old 01-18-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 21 December 2014, 8:10 AM EST
Location: Amsterdam
Posts: 9,694
Thanks: 300
Thanked 2,491 Times in 2,230 Posts
Something like this?

Code:
cd /data
ls | awk -F_ '{A[$1 FS $2]++} END{for(i in A) print i, A[i]}'

Sponsored Links
    #3  
Old 01-18-2013
piynik piynik is offline
Registered User
 
Join Date: Jun 2012
Last Activity: 26 February 2013, 3:52 AM EST
Posts: 55
Thanks: 4
Thanked 0 Times in 0 Posts
would you minding explaining the script so I could adapt it accordingly and test?
    #4  
Old 01-18-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 21 December 2014, 8:10 AM EST
Location: Amsterdam
Posts: 9,694
Thanks: 300
Thanked 2,491 Times in 2,230 Posts
Sure:

Code:
awk -F_ '                     # set field separator to underscore
  {
    A[$1 FS $2]++             # count the number of times $1 FS $2 occurs (field 1 and field2 separated by an underscore (for example "data_1")
  } 
END{
  for(i in A) print i, A[i]   # At the end print the results
}
'


Last edited by Scrutinizer; 01-20-2013 at 02:29 PM..
Sponsored Links
    #5  
Old 01-20-2013
piynik piynik is offline
Registered User
 
Join Date: Jun 2012
Last Activity: 26 February 2013, 3:52 AM EST
Posts: 55
Thanks: 4
Thanked 0 Times in 0 Posts
Thanks for your reply. What is


Code:
A[$1 FS $2]++

?

is that an array?

Last edited by Scrutinizer; 01-20-2013 at 02:28 PM..
Sponsored Links
    #6  
Old 01-20-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 21 December 2014, 8:10 AM EST
Location: Amsterdam
Posts: 9,694
Thanks: 300
Thanked 2,491 Times in 2,230 Posts
That is an element in the associative array A with index $1 FS $2 (FS="_" , so this means $1_$2 ) that gets incremented by 1 ( ++ )
Sponsored Links
    #7  
Old 01-28-2013
piynik piynik is offline
Registered User
 
Join Date: Jun 2012
Last Activity: 26 February 2013, 3:52 AM EST
Posts: 55
Thanks: 4
Thanked 0 Times in 0 Posts
The code has worked impressively, many thanks for that.

I want to write a line to determine that, if the number of files with any prefix is more than 5, then print out the prefix names in one line (separated by a single space), such as

data_1 data_2

I wrote this line


Code:
ls | awk -F_ '{A[$1 FS $2]++} END {for (j in A) {if (A[j] > 5) {printf j, " "}}}'

However the output from this line is


Code:
data_1data_2

It doesn't seem to recognise the single space I asked for between the prefix. Do you know what I may have done wrong?

Last edited by Scrutinizer; 01-28-2013 at 07:50 AM.. Reason: code tags
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to count number of files in directory and write to new file with number of files and their name? Akshay Hegde Shell Programming and Scripting 20 12-10-2012 09:05 AM
Count the number of words in some subset of file and disregard others shoaibjameel123 Shell Programming and Scripting 3 05-03-2011 01:50 AM
Count number of files in directory excluding existing files ammu UNIX for Dummies Questions & Answers 3 08-25-2010 02:54 PM
count number of files in a directory finalight Shell Programming and Scripting 5 05-14-2009 08:57 AM
Count the number of files in a directory Raynon Shell Programming and Scripting 14 08-16-2007 11:07 PM



All times are GMT -4. The time now is 09:27 AM.