Make grep -c display like grep -n?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Make grep -c display like grep -n?
# 1  
Old 08-24-2006
Make grep -c display like grep -n?

Hey Guys,

Wondering if there is a way to do the following

I have a file called test.txt
Code:
abc
def
abc
abc
def

I have a pattern file called pattern.txt containing the following
Code:
abc
def

I want to do a count, but have it display the count value preceeding each line like grep -n

so

grep -n -f pattern.txt would show

Code:
1:abc
2:def
3:abc
4:abc
5:def

What I would like to do is have grep count the number of occurances in the file and place that value at the begining of the output so it would look like

Code:
3:abc
2:def

3 means abc occured 3 times in the file
2 means def occured 2 times in the file

Is there a function or a clever way to achieve this?

Thanks in Advance
# 2  
Old 08-24-2006
I don't know if you could do this without reading the file line-by-line, but here goes:
Code:
while read pattern; do
   count=$(grep -c $pattern test.txt); 
   echo $count:$pattern; 
done < pattern.txt

# 3  
Old 08-25-2006
Can't get grep to do all the work but a quick little script can do it

Say the script name is showdetails.sh

Usage;
showdetails /path_to/pattern.file /path_to/test.txt

Inside the script....

#!/usr/bin/sh

count=1
for nxt in `cat $1`
do
out[$count]="`grep -c $nxt $2`:$nxt\n"
count=`expr $count + 1`
done
echo ${out[@]} | tr -d "[:blank:]"

XX - END SCRIPT --

The tr will remove the blanks in the out put to get a nice neat colum, try it with the tr function and see.

That should work, if my typing is ok...

Goot go and do some firm upgrades in linux.... wish me luck..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Using grep to make a header

How would I do the following : Records other than ”ATOM”,”CONNECT”, ”HETATM”, ”TER” and ”END” are considered header records which describe the metadata about the molecule. Use grep to generate the header. I have this chemistry database. On the attachment. But I am not sure how to use... (4 Replies)
Discussion started by: homeylova223
4 Replies

2. Shell Programming and Scripting

Inconsistent `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l`

i have this line of code that looks for the same file if it is currently running and returns the count. `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l` basically it is assigned to a variable ISRUNNING=`ps -eaf -o args | grep -i sfs_pcard_load_file.ksh |... (6 Replies)
Discussion started by: wtolentino
6 Replies

3. UNIX for Advanced & Expert Users

find/grep two items and display both

I have a file against which I can grep a string for. I can also check for that string count using wc -l (or grep -c). I need to display the results of both in one output i.e. 'line containing string' and 'count' - what would be the most efficient way of managing this? Thanks in advance. (3 Replies)
Discussion started by: haider1
3 Replies

4. UNIX for Dummies Questions & Answers

how to make this grep command

/usr/xpg4/bin/grep -e "Type" / datarecords.txt output datarecords.txt: male | datarecords.txt: male | datarecords.txt: female i wanna the output to be :male | :male | :female at the end not to appear the filename fom grep command :D :D (3 Replies)
Discussion started by: teefa
3 Replies

5. AIX

How to make grep stop at first match

I use grep to check for a string that validates data in a file, it works great but the problem is that the file is becoming too big and gerp has started hurting the response time to users. Since I only need to find the first occurrence I have been looking for ways to stop grep for scanning the rest... (8 Replies)
Discussion started by: mpoblete
8 Replies

6. Solaris

grep and display few lines before and after

Hi is there a way in grep to display few lines before and after the pattern?? I tried options A and B and after-context and before-context. But they don't work on Solaris platform. please advise. (13 Replies)
Discussion started by: melanie_pfefer
13 Replies

7. Shell Programming and Scripting

grep display word only

Folks, is it possible to display only words with grep (or any built-in ultility)? I have more than 1 pattern to search, say apple & orange The text goes like this: So I need to display all the words starting with apple or orange The output should be: Any idea? (7 Replies)
Discussion started by: bsddaemon
7 Replies

8. Shell Programming and Scripting

How to Grep for particular word and display..

Hi Guru's.... I've one log file in all my systems which writes the backup information.. I'have written a command like this: ssh -l ora${sid} ${primaryhost} "tail -50 /oracle/$ORACLE_SID/newbackup/END_BACKUP.log" |grep 'insert' |tail -1| awk '{print $7}' We have nearly 50 systems in our... (2 Replies)
Discussion started by: suri.tyson
2 Replies

9. UNIX for Dummies Questions & Answers

How to display first 7 char of grep results?

My file contains the following: uat2000.aspclient.active=true uat2001.aspclient.active=true uat2002.aspclient.active=true uat2003.aspclient.active=true uat2004.aspclient.active=false uat2005.aspclient.active=false uat2006.aspclient.active=false uat2007.aspclient.active=false... (8 Replies)
Discussion started by: kthatch
8 Replies

10. UNIX for Dummies Questions & Answers

Grep and display

I have the following data: A 1 2 3 A 4 5 6 A 7 8 9 I want to grep this data with A and 3 lines below it then display them in this format: A 1 2 3 A 4 5 6 A 7 8 9 (4 Replies)
Discussion started by: bobo
4 Replies
Login or Register to Ask a Question