The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
grep and display few lines before and after melanie_pfefer SUN Solaris 8 2 Weeks Ago 08:05 AM
grep a word and display its column Orbix UNIX for Dummies Questions & Answers 1 12-24-2007 04:32 PM
How to display first 7 char of grep results? kthatch UNIX for Dummies Questions & Answers 8 04-04-2007 10:00 PM
using cat and grep to display missing records jxh461 UNIX for Dummies Questions & Answers 4 01-13-2007 05:56 AM
Grep and display bobo UNIX for Dummies Questions & Answers 4 06-19-2006 11:42 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-24-2006
Registered User
 

Join Date: May 2006
Posts: 5
Stumble this Post!
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 08-24-2006
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,323
Stumble this Post!
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
Reply With Quote
  #3 (permalink)  
Old 08-24-2006
Registered User
 

Join Date: Aug 2006
Posts: 122
Stumble this Post!
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..
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:58 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0