Question: I have been working on a script which will get the number of occurrences of a String . So the file gets the contents as 

```
INFO STRING:Unexpected exception while retrieving monitors for server
INFO STRING:Unexpected exception while retrieving monitors for server
WARN STRING:service end point is down..
WARN STRING:service end point is down..
INFO STRING:java.net.SocketTimeoutException: Read timed out
INFO STRING:java.sql.SQLException
INFO STRING:java.sql.SQLException

```

These values are obtained at run time . Now what i need is i need to write a script which will find out the number of occurences of a string like say 
INFO STRING:java.sql.SQLException is 2. The file contents are obtained dynamically and will change . I need to pick the value for the first ALERT STRING and then check the occurrences count to a variable and save that to a file.

Please suggest me on how to move on this

Answer: 
The `uniq` command can count repeated, *continuous* groups of lines. First, extract just the parts you want with `awk`, sort them (important), then pipe to `uniq`.

```
awk '{print $1, $2}' logfile.txt | sort | uniq -c

```
With grep:

```
grep -o 'String' file |wc -l

```