|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 05:53 AM.. Reason: Code tags, please... |
| Sponsored Links | ||
|
|
#2
|
||||
|
||||
|
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
|
|||
|
|||
|
would you minding explaining the script so I could adapt it accordingly and test?
|
|
#4
|
||||
|
||||
|
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 01:29 PM.. |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
Thanks for your reply. What is Code:
A[$1 FS $2]++ ? is that an array? Last edited by Scrutinizer; 01-20-2013 at 01:28 PM.. |
| Sponsored Links | |
|
|
#6
|
||||
|
||||
|
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
|
|||
|
|||
|
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 06:50 AM.. Reason: code tags |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 08:05 AM |
| Count the number of words in some subset of file and disregard others | shoaibjameel123 | Shell Programming and Scripting | 3 | 05-03-2011 12:50 AM |
| Count number of files in directory excluding existing files | ammu | UNIX for Dummies Questions & Answers | 3 | 08-25-2010 01:54 PM |
| count number of files in a directory | finalight | Shell Programming and Scripting | 5 | 05-14-2009 07:57 AM |
| Count the number of files in a directory | Raynon | Shell Programming and Scripting | 14 | 08-16-2007 10:07 PM |
|
|