Search for a pattern in a String file and count the occurance of each pattern


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for a pattern in a String file and count the occurance of each pattern
# 1  
Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR-[0-9][0-9][0-9][0-9][0-9] in a file and return a count for each of the error reported

Input file is a free flowing file without any format

example of output
ERR-00001=5
....
ERR-01010=10
.....
ERR-99999=10
# 2  
Code:
grep -o "ERR-[0-9][0-9][0-9][0-9][0-9]" input_file| sort | uniq -c

This User Gave Thanks to Yoda For This Post:
# 3  
Hi Bipinajith,

Thanks , for your prompt reply.
I am getting a message
grep: illegal option -- o

I am using ksh

The output without the -o option also seems to work, what are we trying to do by the -o option .

I am new to shell please pardon my ignorance
# 4  
The -o option prints only the strings matching the pattern, each on a separate line. I think this is available only with GNU grep.
You may try this awk script:
Code:
awk '{while(match($0,/ERR-[0-9]{5}=/))
{
 c[substr($0,RSTART,RLENGTH-1)]++
 sub(/ERR-[0-9]{5}=/,"")
}}END{for(i in c) print i "=" c[i]}' file

This User Gave Thanks to elixir_sinari For This Post:
# 5  
Code:
The modified script, couple of typos  removed
awk '
{while(match($0,/ERR-[0-9]{5}/))
    { 
        c[substr($0,RSTART,RLENGTH)]++ sub(/ERR-[0-9]{5}/,"") 
     }
} END
{for(i in c) print i "=" c[i]}' $inputFile


Last edited by Franklin52; 11-06-2012 at 04:40 AM.. Reason: Please use code tags for data and code samples
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #951
Difficulty: Medium
The first known description of a self-reproducing computer program in science fiction was in a short story written in 1970.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Identify file pattern, take count of pattern, then act

Guys - Need your ideas on a section of code to finish something up. To make a long story short, I'm parsing a print output file that goes to pre-printed forms. I'm intercepting it, parsing it, formatting it, cutting it up into individual pages, grabbing the text I want in zones, building an... (3 Replies)
Discussion started by: ampsys
3 Replies

2. Shell Programming and Scripting

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

3. Shell Programming and Scripting

sed pattern to delete lines containing a pattern, except the first occurance

Hello sed gurus. I am using ksh on Sun and have a file created by concatenating several other files. All files contain header rows. I just need to keep the first occurrence and remove all other header rows. header for file 1111 2222 3333 header for file 1111 2222 3333 header for file... (8 Replies)
Discussion started by: gary_w
8 Replies

4. Shell Programming and Scripting

Awk search for string pattern in delimited file

I've got a semicolon delimited file. I would like to search for fields that match a pattern, and not hardcoded eg "mth". *th=something If the delimited field fulfills this condition, eg. mth=value I would like to print out both key and value for some number comparison. eg. if value > "12"... (5 Replies)
Discussion started by: alienated
5 Replies

5. UNIX for Dummies Questions & Answers

Search and Count Occurrences of Pattern in a File

I need to search and count the occurrences of a pattern in a file. The catch here is it's a pattern and not a word ( not necessarily delimited by spaces). For eg. if ABCD is the pattern I need to search and count, it can come in all flavors like (ABCD, ABCD), XYZ.ABCD=100, XYZ.ABCD>=500,... (6 Replies)
Discussion started by: tektips
6 Replies

6. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies

7. Shell Programming and Scripting

Finding Last occurance of another pattern when a pattern is found.

Hi, I have two files viz, rak1: $ cat rak1 rak2: $ cat rak2 sdiff rak1 rak2 returns: I want the lines that got modified, changed, or deleted preceding with the section they are in. I have done this so far: (1 Reply)
Discussion started by: rakeshou
1 Replies

8. UNIX for Dummies Questions & Answers

Count of matched pattern occurance

In a file a pattern is occured many times randomly. Even it may appear more then once in the same line too. How i can get the number of times that pattern appeared in the file? let the file name is abc.txt and the pattern is "xyz". I used the following code: grep -ic "xyz" abc.txt but it is... (3 Replies)
Discussion started by: palash2k
3 Replies

9. Shell Programming and Scripting

Search Mulitiple String pattern in a file

Hi, I need to search for a multiple string pattern(5 key words) in a file(similar to a flat file) ,and i need to store the output in a another file . In that file we may have mutiple occurrences of the key words.and i need only the unique. kindly help out. Thanks, Mohana Krishnan (2 Replies)
Discussion started by: krishnan_6015@y
2 Replies

10. Shell Programming and Scripting

Search file for pattern and grab some lines before pattern

I want to search a file for a string and then if the string is found I need the line that the string is on - but also the previous two lines from the file (that the pattern will not be found in) This is on solaris Can you help? (2 Replies)
Discussion started by: frustrated1
2 Replies

Featured Tech Videos