Group and count file by date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Group and count file by date
# 1  
Old 08-18-2009
Group and count file by date

Hi all,
in BIN/SH I need to group and count files by date.

Ie:
Code:
ls -la
 
-rw-r--r--   1 aaa dba 122 Jul 13 14:28 as1.tmp
-rw-r--r--   1 aaa dba 122 Jul 13 15:27 as2.tmp
-rw-r--r--   1 aaa dba 122 Jul 21 17:04 as3.tmp
-rw-r--r--   1 aaa dba 122 Jul 23 15:45 as4.tmp
-rw-r--r--   1 aaa dba 122 Jul 23 17:26 as5.tmp
-rw-r--r--   1 aaa dba 122 Jul 27 18:39 as6.tmp
-rw-r--r--   1 aaa dba 122 Jul 28 10:53 as7.tmp
-rw-r--r--   1 aaa dba 122 Jul 28 18:07 as8.tmp
-rw-r--r--   1 aaa dba 122 Jul 29 16:54 as9.tmp
-rw-r--r--   1 aaa dba 122 Jul 30 16:33 as11.tmp
-rwxr-xr-x   1 aaa dba 122 Jan 12  2009 1*
-rwxr-xr-x   1 aaa dba 122 Jan 12  2009 2*
-rwxr-xr-x   1 aaa dba 122 Jan  8  2009 3*
-rwxr-xr-x   1 aaa dba 122 Jan  8  2008 4*
-rwxr-xr-x   1 aaa dba 122 Jan 21  2009 aaw.sh

Should be:
Code:
 8 Jan 2008 1
 8 Jan 2009 1
12 Jan 2009 2
21 Jan 2009 1
13 Jul 2009 2
21 Jul 2009 1
23 Jul 2009 2
27 Jul 2009 1
28 Jul 2009 2
29 Jul 2009 1
30 Jul 2009 1

Should be better this one:
Code:
20080108 1
20090108 1
20090112 2
20090121 1
20090613 2
20090621 1
20090623 2
20090627 1
20090628 2
20090629 1
20090630 1



Thanks a lot,
Riccardo
# 2  
Old 08-18-2009
something like this will do i guess..
Code:
ls -l|awk '$8 ~ /..:../{gsub($8,"2009")}1'|awk '{A[$7" "$6" "$8]++}END{for (i in A){print i" "A[i]}}'

# 3  
Old 08-18-2009
Quote:
Originally Posted by vidyadhar85
something like this will do i guess..
Code:
ls -l|awk '$8 ~ /..:../{gsub($8,"2009")}1'|awk '{A[$7" "$6" "$8]++}END{for (i in A){print i" "A[i]}}'

vidyadhar85, I don't get the desired ouput with your solution....

ric79, try this:

Code:
ls -la |
awk ' BEGIN{
  split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", month, " ")
  for (i=1; i<=12; i++) mdigit[month[i]]=i
}
$8 ~ /:/{$8="2009"}
{
  dat=$8 sprintf("%02d",mdigit[$6]) sprintf("%02d",$7)
  a[dat]++
}
END{for(i in a){print i, a[i]|"sort"}}'

Regards
# 4  
Old 08-18-2009
Below command can give you the same result


Code:
stat -c "%y" * |cut -d' ' -f1|sort|uniq -c

will give you date like 2009-05-26

Code:
stat -c "%y" * |cut -d' ' -f1|sort|uniq -c|tr -d "-"

will give you date like 20090526

Code:
stat -c "%y" * .*|cut -d' ' -f1|sort|uniq -c|tr -d "-"

will include hidden files also

Regards,

Ranjith
# 5  
Old 08-18-2009
Code:
ls -la |
awk ' BEGIN{
  split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", month, " ")
  for (i=1; i<=12; i++) mdigit[month[i]]=i
}
$8 ~ /:/{$8="2009"}
{
  dat=$8 sprintf("%02d",mdigit[$6]) sprintf("%02d",$7)
  a[dat]++
}
END{for(i in a){print i, a[i]|"sort"}}'

1) Is it possibile to avoid "2009" and get the current year?
2) The script display also "0000 1". I need to count only Files and not Directories
3) Jan Feb Mar ... what does it happen if the unix version is Italian? Is there a way to make ls printing 01 instead of Jan?

Thanks,
Riccardo
# 6  
Old 08-18-2009
Here I use a variabale for the year and the directories are ignored now with !/^-/{next}.
For another language you have to change the month names manualy.

Code:
ls -la |
awk -v year=$(date "+%Y")' BEGIN{
  split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", month, " ")
  for (i=1; i<=12; i++) mdigit[month[i]]=i
}
!/^-/{next}
$8 ~ /:/{$8=year}
{
  dat=$8 sprintf("%02d",mdigit[$6]) sprintf("%02d",$7)
  a[dat]++
}
END{for(i in a){print i, a[i]|"sort"}}'

Regards
# 7  
Old 08-18-2009
Code:
#!/bin/sh
ls -la |
awk -v year=$(date "+%Y") ' BEGIN{
  split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", month, " ")
  for (i=1; i<=12; i++) mdigit[month[i]]=i
  $mydate="2009"
}
!/^-/{next}
$8 ~ /:/{$8=year}
{
  dat=$8 sprintf("%02d",mdigit[$6]) sprintf("%02d",$7)
  a[dat]++
}
END{for(i in a){print i, a[i]|"sort"}}'

gives me
syntax error at line 3: `(' unexpected

Last edited by ric79; 08-18-2009 at 11:58 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Find the count of files by last created date based on the given date range

My unix version is IBM AIX Version 6.1 I tried google my requirement and found the below answer, find . -newermt “2012-06-15 08:13" ! -newermt “2012-06-15 18:20" But newer command is not working in AIX version 6.1 unix I have given my requirement below: Input: atr files: ... (1 Reply)
Discussion started by: yuvaa27
1 Replies

2. Shell Programming and Scripting

awk Group By and count string occurrences

Hi Gurus, I'm scratching my head over and over and couldn't find the the right way to compose this AWK properly - PLEASE HELP :confused: Input: c,d,e,CLICK a,b,c,CLICK a,b,c,CONV c,d,e,CLICK a,b,c,CLICK a,b,c,CLICK a,b,c,CONV b,c,d,CLICK c,d,e,CLICK c,d,e,CLICK b,c,d,CONV... (6 Replies)
Discussion started by: Royi
6 Replies

3. Shell Programming and Scripting

Find and count unique date values in a file based on position

Hello, I need some sort of way to extract every date contained in a file, and count how many of those dates there are. Here are the specifics: The date format I'm looking for is mm/dd/yyyy I only need to look after line 45 in the file (that's where the data begins) The columns of... (2 Replies)
Discussion started by: ronan1219
2 Replies

4. Shell Programming and Scripting

Help me to perform count & group by operation in shell scripting?

Hi All, I want to display the distinct values in the file and for each distinct value how may occurance or there. Test data: test1.dat 20121105 20121105 20121105 20121105 20121106 20121106 20121106 20121105 I need to display the output like Output (2 Replies)
Discussion started by: bbc17484
2 Replies

5. Shell Programming and Scripting

Count of matched pattern occurences by minute and date in a log file

Anyone knows how to use AWK to achieve the following Sun Feb 12 00:41:01-00:41:59 Success:2 Fail:2 Sun Feb 12 00:42:01-00:42:59 Success:1 Fail:2 Sun Feb 12 01:20:01-01:20:59 Success:1 Fail:2 Mon Feb 13 22:41:01-22:41:59 Success:1 Fail:1 log file: Success Success Fail Fail ... (9 Replies)
Discussion started by: timmywong
9 Replies

6. Shell Programming and Scripting

Search, group , print count

hi All, need help. have a file like below A, error in 123 B, log files are present A, error in 23444 B, log files are present A, move to next line C, matching messages -- expected output-- A , count =2 , error in * A , count =1 , move to next line B , count =2 , log files are present... (2 Replies)
Discussion started by: arun1401
2 Replies

7. Shell Programming and Scripting

split file based on group count

Hi, can some one please help me to split the file based on groups. like in the below scenario x indicates the begining of the group and the file should be split each with 2 groups below there are 10 groups it should create 5 files. could you please help? (4 Replies)
Discussion started by: hitmansilentass
4 Replies

8. UNIX for Dummies Questions & Answers

Grep group by and count

Hi, I have several files with same filename pattern. I want to calculate count of individual files using grep/egrep. Let me be more descriptive In directory E1 i have files like ab_20091201_12:24 ab_20091201_03:24 cd_20091201_04:16 cd_20091203_08:34 ef_20091201_06:12 ef_20091201 Now i want... (3 Replies)
Discussion started by: shounakboss
3 Replies

9. Shell Programming and Scripting

Awk-Group count of field

Hi, Suppose if i am having a file with following records as given below. 5555 6756 5555 4555 4555 6767 how can i get the count of each record using AWK. Eg:5555 count should be 2 4555 count should be 2 6767 count should be 1 ... (5 Replies)
Discussion started by: tinivt
5 Replies

10. Shell Programming and Scripting

Rename a file to have Date and Line Count

Hi, I am trying to come with one line command which will rename a file to have Date (MMDDYYYY) and Line Count in the file. Any lead will be appreciated rgds bmk (2 Replies)
Discussion started by: bmkux
2 Replies
Login or Register to Ask a Question