The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 13 02-19-2009 11:54 PM
grep a word and display its column Orbix UNIX for Dummies Questions & Answers 1 12-24-2007 08:32 PM
How to display first 7 char of grep results? kthatch UNIX for Dummies Questions & Answers 8 04-05-2007 01:00 AM
using cat and grep to display missing records jxh461 UNIX for Dummies Questions & Answers 4 01-13-2007 09:56 AM
Grep and display bobo UNIX for Dummies Questions & Answers 4 06-19-2006 02:42 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-24-2006
Jerrad Jerrad is offline
Registered User
  
 

Join Date: May 2006
Posts: 7
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 (permalink)  
Old 08-24-2006
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
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 (permalink)  
Old 08-25-2006
Andrek Andrek is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 122
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..
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:29 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0