grep to give how many times each lines were found


 
Thread Tools Search this Thread
Operating Systems AIX grep to give how many times each lines were found
# 1  
Old 12-13-2006
grep to give how many times each lines were found

Lets say I have a file containing string patterns to be looked for inside a file.

I would normaly do :

grep -if MyFilePattern FiletoSearchInto

if I use the -c it gives how many total lines it found out of my whole pattern file, but what if i want grep to report how many times it found each records in the MyFilePattern file ?

This is to help me at my job to make sure whenever I do this, that I would either find each lines once or none at all. If one line is there more then once, then I have a problem that I must fix.

I just don't remember how to do it with grep.
# 2  
Old 12-13-2006
Code:
while read str
do
   if grep -q "$str" FiletoSearchInto
   then
       echo $str
   fi
done < MyFilePattern

# 3  
Old 12-14-2006
What does the grep -q returns as value ?

can I do "if (grep -q ....) > 1" ???

I want anything that has been found more then once. If found once or not at all, then I don't want to know it.
# 4  
Old 12-14-2006
Quote:
Originally Posted by Browser_ice
What does the grep -q returns as value ?

can I do "if (grep -q ....) > 1" ???

I want anything that has been found more then once. If found once or not at all, then I don't want to know it.
-q gives the status of 0 if the match is found in the file else gives the status of 1.

try this
Code:
while read str
do
   if [ $(grep -c "$str" FiletoSearchInto) -gt 1 ]
   then
       echo $str
   fi
done < MyFilePattern

# 5  
Old 12-14-2006
The -q option of grep means "quiet": with this option grep gives a returncode 0 if it found the pattern and 1 of not. Additionally one doesn't have to get rid of the output as there is none. To use grep -q in a compound command use

Code:
if [ $(InputStream | grep -q "pattern" ; print - $?) -eq 0 ] ; then
     # pattern found
else
     # pattern not found
fi

Here is a little script, which accepts a filename and a pattern (in principle any regexp should work, but i haven't tested it with more complicated ones - there may arise problems with the shell interpreting it in this case) and prints out the number of occurrences:

Code:
#!/bin/ksh

fIn="$1"
chPattern="$2"
iFound=0

cat $fIn | while read chLine ; do
     while [ $(print - $chLine | grep -c "$chPattern") -gt 0 ] ; do
          chLine="$(print - "$chLine" | sed 's/'"$chPattern"'//')"
          (( iFound += 1 ))
     done
done

print - "Occurrences: $iFound"

The script makes use of a certain feature of sed: to change only the first ocurrence of the pattern in a substitute-operation, as long as the g-clause is not given. The command

Code:
print - "pattern pattern pattern" | sed '/pattern/newpattern/'

will only change the first occurrence of "pattern", to change all one would have to write:

Code:
print - "pattern pattern pattern" | sed '/pattern/newpattern/g'

The inner loop "chops off" one occurrence of pattern after the other, until none is left. For every chop-off-operation a counter is increased.

bakunin

Last edited by bakunin; 12-14-2006 at 09:59 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Newbie looking for how to Grep times more than 10 seconds apart

I am new to grep and Linux and am looking to see if grep can parse out a list of lines that have a difference of more than 10 seconds between the times on each line. Example 2016-09-17 19:30:57 INFO: id: 4562079216, time: 2016-09-17 19:30:41, 2016-09-17 12:02:26 INFO: id:... (26 Replies)
Discussion started by: Markham
26 Replies

2. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

3. Shell Programming and Scripting

How to print few lines before and after matching word is found suing grep?

Hi, here are few lines present in the logs. I want to grep on Error and print few lines before and after Error word is found line1 Line2 Line3 Error Line4 Line5 Line6 Line7 I want the output to be Line2 Line3 Error Line5 (1 Reply)
Discussion started by: arghadeep adity
1 Replies

4. HP-UX

Getting the lines that contains a '/' N times(N given at runtime)

Hi all, The task I have sounds easy enough but my solution seems too much complicated and I would like some ideas/feedback on how to achieve the same goal with a more elegant solution on HP-UX B.11.23 (a grep with a regexp would be nice, I did not manage to do a working one :/ ) Here is my... (4 Replies)
Discussion started by: bibou25
4 Replies

5. Shell Programming and Scripting

Search for file, give error if more than one file is found

Want to write a function that prints an error when passed a list of file names. If the file list is empty, print error "no file found", if there are more than one file, print "error more than one file found" (22 Replies)
Discussion started by: kristinu
22 Replies

6. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

7. HP-UX

grep for x for m times ',\{11\}'

All: OS version HP-UX ga016a501 B.11.31 U ia64 from the command prompt -grep for 1 to 11 occurences of "," returns both rows from the command prompt -grep for 11 occurences of "," returns 0 rows - should be 1 row. Any ideas - why? ga016a501 -> grep ',\{1,11\}' test3 | more ... (7 Replies)
Discussion started by: Bill L.
7 Replies

8. Shell Programming and Scripting

read the first 10 lines below a keyword found by grep

Hello Everyone, i need to read specific number of lines ( always serialized ; i.e from 10 to 20 or from 34 to 44 ) in a file , where the first line is found by grep 'ing a keyword. example file.txt ------------------------------------------------------------------ --header this is the... (7 Replies)
Discussion started by: alain.kazan
7 Replies

9. UNIX for Dummies Questions & Answers

Grep and display n lines after the match is found.

Hello, How do I use grep to find a pattern in a list of file and then display 5 lines after the pattern is matched Eg: I want to match the string GetPresentCode in all files in a folder and then see 4 lines following this match. I am not sure if grep is what should be used to achieve. Thanks!... (3 Replies)
Discussion started by: cv_pan
3 Replies

10. Shell Programming and Scripting

how to grep for lines between 2 give patterns?

Hello shell gurus :) how to grep for lines between 2 give patterns? thanks (1 Reply)
Discussion started by: melanie_pfefer
1 Replies
Login or Register to Ask a Question