Search and count patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and count patterns
# 1  
Old 10-06-2013
Search and count patterns

Hi,
I have a text file the contents are like this
Quote:
p
i
Z [720]
i
e
Z [740]
n_
i
Z [0]
k
k
a
Z [720]
T
T
a
Z [0]
m
i
Z [0]
N
i
Z [0]
k
i
Z [0]
now i want to search patterns Z [720], Z [0] etc and count the occurrence of such patterns, after Z value can be any random digits, please help me it is urgent...

output like this
Z [0].............>5
Z [33]............>8

Last edited by sreejithalokkan; 10-06-2013 at 06:53 AM.. Reason: to improve the meaning of post
# 2  
Old 10-06-2013
Try:
Code:
egrep -c '^Z \[[0-9]+\]' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 10-06-2013
Quote:
Originally Posted by bartus11
Try:
Code:
egrep -c '^Z \[[0-9]+\]' file

thanks it shows total occurrence i want the occurrence of each pattern
# 4  
Old 10-06-2013
Try:
Code:
awk '/^Z \[[0-9]+\]/{A[$0]++} END{for(i in A) print i " .....> " A[i]}' file

---edit----

More robust version:
Code:
awk '{$1=$1} /^Z \[[0-9]+\]/{A[$0]++} END{for(i in A) print i " .....> " A[i]}' file


Last edited by Scrutinizer; 10-06-2013 at 07:49 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 10-06-2013
THANKS scrutinizer it worked wonderfully
# 6  
Old 10-06-2013
one more version
try

Code:
$ awk '/^Z/{_[$1$2]++}END{for (i in _)print i,_[i]}' OFS=\\t file

resulting
Code:
Z [740]    1
Z [0]      5
Z [720]    2

---------- Post updated at 05:46 AM ---------- Previous update was at 05:42 AM ----------

Quote:
Originally Posted by Scrutinizer
Try:
Code:
awk '/^Z \[[0-9]+\]/{A[$0]++} END{for(i in A) print i " .....> " A[i]}' file

its producing

Code:
$ awk '/^Z \[[0-9]+\]/{A[$0]++} END{for(i in A) print i " .....> " A[i]}' file
Z [740] .....> 1
Z [0] .....> 4
Z [720] .....> 2
Z [0]  .....> 1


Last edited by Akshay Hegde; 10-06-2013 at 07:57 AM..
These 2 Users Gave Thanks to Akshay Hegde For This Post:
# 7  
Old 10-06-2013
Yes I realized it was not so robust, if there is any spurious spacing. I had added a more robust version to my post..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for patterns on different lines

im using the following code to search a log for entries on two different lines: awk 'BEGIN{count=0} /'"${firstpattern}"'/,/'"${secondpattern}"'/ { print; if ($0 ~ /'"${thirdpattern}"'/){count++}; } END { print count }' data.txt firstpattern="start error log" secondpattern="i am logging the... (1 Reply)
Discussion started by: SkySmart
1 Replies

2. Shell Programming and Scripting

awk to print before and after lines then count of patterns

What i'm trying to do here is show X amount of lines before and after the string "serialNumber" is found. BEFORE=3 AFTER=2 gawk '{a=$0} {count=0} /serialNumber/ && /./ {for(i=NR-'"${BEFORE}"';i<=NR;i++){count++ ;print a}for(i=1;i<'"${AFTER}"';i++){getline; print ; count ++; print... (5 Replies)
Discussion started by: SkySmart
5 Replies

3. Shell Programming and Scripting

Awk to Count Multiple patterns in a huge file

Hi, I have a file that is 430K lines long. It has records like below |site1|MAP |site2|MAP |site1|MODAL |site2|MAP |site2|MODAL |site2|LINK |site1|LINK My task is to count the number of time MAP, MODAL, LINK occurs for a single site and write new records like below to a new file ... (5 Replies)
Discussion started by: reach.sree@gmai
5 Replies

4. Shell Programming and Scripting

count the number of occurring patterns in a file.

Hi, I have a file with a '|' pipe delimeter. I want to find number of counts for a particular pattern in particular field. Is it possible to do it in a single command? 1) want to find total number of "0" in field 4. 2) want to find total number of different records in field 4 ( similar to... (5 Replies)
Discussion started by: rudoraj
5 Replies

5. Shell Programming and Scripting

Search by patterns case

42 network read failed sv1 sv23 sv4 sv11 sv23 sv5 sv 7 48 client hostname could not be found sv21 sv78 sv19 sv22 sv111 sv203 sv5 sv 33 49 client did not start sv1 sv21 54 timed out connecting to client sv2 sv4 sv12 above is my file , I'd like to use a script to list all name... (5 Replies)
Discussion started by: Sara_84
5 Replies

6. Shell Programming and Scripting

Search for the two patterns and print everything in between

Hi all, I have a file having data: @HWUSI-EAS1727:19:6:1:3674:984:0:1#GTTAATA NTTGGGTTTTCT @HWUSI-EAS1727:19:6:1:3674:984:0:1#GTTA... NTTGGGTTTTCT @HWUSI-EAS1727:19:6:1:3674:984:0:1#.....CT NTTGGGTTTTCT I want to print everything starting from # till line ends. can you please help me how... (5 Replies)
Discussion started by: pirates.genome
5 Replies

7. UNIX for Advanced & Expert Users

Count number of unique patterns from a log file

Hello Everyone I need your help in fixing this issue., I have a log file which has data of users logging in to an application. I want to search for a particular pattern in the log ISSessionValidated=N If this key word is found , the above 8 lines will contain the name of the user who's... (12 Replies)
Discussion started by: xtechkid
12 Replies

8. Shell Programming and Scripting

search multiple patterns

I have two lists in a file that look like a b b a e f c d f e d c I would like a final list a b c d e f I've tried multiple grep and awk but can't get it to work (8 Replies)
Discussion started by: godzilla07
8 Replies

9. Shell Programming and Scripting

Count lines between two patterns inside a file

Hi, Im doing a script to find the number of lines included inside a file newly. These lines are in between #ifdef FLAG1 and #else or #endif or #else and #endif. I tried like this, awk '/#ifdef Flag1/,/#e/{print}' aa.c | wc -l awk '/#ifndef Flag1/,/#endif/{print}' aa.c | awk... (6 Replies)
Discussion started by: priyadarshini
6 Replies

10. Shell Programming and Scripting

search patterns

hello, i have an input file of about 50,00,000 lines. few of its lines are as follows: <CR:0023498789,TPO-14987084;BO=IC&SUB=ALLP <CF:0023498789,CB=YES;BIL&NC=NO <CF:0023498789,CW=NO;NS=NO <GC:0023498789,CG=YES;TPO&NC=YES <CR:0024659841,TPO-14484621;BO=NO&BA=OC&SUB=ALLH... (1 Reply)
Discussion started by: rochitsharma
1 Replies
Login or Register to Ask a Question