Grep group by and count


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep group by and count
# 1  
Old 01-11-2010
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
Code:
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 to calculate how many files are there for ab, cd and ef. I used something like this
Code:
ls -lrt | egrep "ab_20091201|cd_20091201|ef_20091201" | wc -l

but this is giving me the sum of files. I want individual file count with single command. Any suggestions would be helpful.

Last edited by Scott; 01-11-2010 at 04:35 PM.. Reason: Added code tags
# 2  
Old 01-11-2010
Grep group by and count

Code:
for i in `ls -1 | grep "20091201" | nawk -F"_" '{print $1}'| sort -u`; do
  print $i=`ls -lrt|grep $i"_20091201"|wc -l` ; done


Last edited by Scott; 01-11-2010 at 04:34 PM.. Reason: Added code tags
# 3  
Old 01-11-2010
Try:
Code:
ls | awk -F_ '{A[$1"_"$2]++}END{for (i in A) print i,A[i]}'

Output:
Code:
ef_20091201 2
cd_20091203 1
b_20091201 1
cd_20091201 1
ab_20091201 2

# 4  
Old 01-11-2010
Code:
egrep -c "ab_20091201|cd_20091201|ef_20091201" /E1

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep patterns and group counts

Hi, I have a continuous log file which has the following format:- 02/Sep/2015: IP 11.151.108.166 error occurred etc 03/Sep/2015: IP 11.151.108.188 error occurred etc 03/Sep/2015: IP 11.152.178.250 error occurred etc 03/Sep/2015: IP 11.188.108.176 error occurred etc 03/Sep/2015: IP... (4 Replies)
Discussion started by: finn
4 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

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

4. Shell Programming and Scripting

need a one liner to grep a group info from /etc/group and use that result to search passwd file

/etc/group tiadm::345:mk789,po312,jo343,ju454,ko453,yx879,iy345,hn453 bin::2:root,daemon sys::3:root,bin,adm adm::4:root,daemon uucp::5:root /etc/passwd mk789:x:234:1::/export/home/dummy:/bin/sh po312:x:234:1::/export/home/dummy:/bin/sh ju454:x:234:1::/export/home/dummy:/bin/sh... (6 Replies)
Discussion started by: chidori
6 Replies

5. 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

6. 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

7. Shell Programming and Scripting

Group and count file by date

Hi all, in BIN/SH I need to group and count files by date. Ie: 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... (8 Replies)
Discussion started by: ric79
8 Replies

8. 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

9. UNIX for Dummies Questions & Answers

Sorting using count, grep and count

Hi, I trying to sort information in a file by making count for every object. For example: A B A D A B C i would like to sort out count for each object (A's, B's and etc) but in actual case i dont know the object but i know the position ofthe object. do i need to use array as well? Any... (2 Replies)
Discussion started by: sukhpal_78
2 Replies

10. UNIX for Dummies Questions & Answers

grep group and passwd file

How can I find find all members in the /etc/password file that belong to the dba group in the /etc/group file? (4 Replies)
Discussion started by: mozartny
4 Replies
Login or Register to Ask a Question