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, XYZ.ABCD = 200 etc.
I tried using something like below for Word search and count ( got if from another post trying to count occurrences of word , but not sure how I could fit this in for a string which is not necessarily delimited.
Example of file contents and two specific search scenarios I am trying to address
Scenarios which I need to address:-
1. Search by ADB_STFC_BAL
Expected Result : Count 9
2. Search by PABC_CUST_ACCT_DETL_CURR.ADB_STFC_BAL
Expected Result : Count 6
Any help with altering the above script to use the pattern or any ather approaches to solve the problem using awk or so are greatly appreciated. Files are pretty large and I need to do this for around 200 words.
Thanks vgersh99..it certainly helps.. don't have nawk in our server tried with awk and it works.. Have a few addl qs as I am not very familiar with awk..
I want to assign this output to a variable say $COUNT. Also assuming that my filename and pattern are stored in $i and $j respectively, how do I modify this same awk command? My intent is to pass the script a filelist and pattern list and loop thru it.
In the above example $i=myfile2.txt and $j=ADB_STFC_BAL
I copied your file to a temp file on my system, called file26.
Note that I matched your first count correctly. Now take a look at the 2nd and 3rd commands. A . in grep means to match any character, so I had to do a \. to escape the . character to precisely match on it.
And 4 & 2 are the correct match counts for your input file.
By the way, I often use this trick of replacing a space character with a new line [the first tr command] when trying to find unique matching.
Input:
|Running the Rsync|Sun Oct 16 22:48:01 BST 2016
|End of the Rsync|Sun Oct 16 22:49:54 BST 2016
|Running the Rsync|Sun Oct 16 22:54:01 BST 2016
|End of the Rsync|Sun Oct 16 22:55:45 BST 2016
|Running the Rsync|Sun Oct 16 23:00:02 BST 2016
|End of the Rsync|Sun Oct 16 23:01:44 BST 2016... (4 Replies)
input
amex-11 10 abc
amex-11 20 bcn
amed-12 1 abc
I tried something like this.
awk '{h++}; END { for(k in h) print k, h }' rm1
output
amex-11 1 10 abc
amex-11 1 20 bcn
amed-12 2 1 abc
Note: The second column represents the occurrences. amex-11 is first one and amed-12 is the... (5 Replies)
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)
Hi,
file1.txt
AAA
BBB
CCC
DDD
file2.txt
abc|AAA|AAAabcbcs|fnwufnq
bca|nwruqf|AAA|fwfwwefwef
fmimwe|BBB|fnqwufw|wufbqw
wcdbi|CCC|wefnwin|wfwwf
DDD|wabvfav|wqef|fwbwqfwfe
i need the count of rows of file1.txt present in the file2.txt
required output:
AAA 2 (10 Replies)
I am trying to search a file for a patterns ERR- 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 (4 Replies)
i have one file, i am doing 'vi Filename' now i want to search for particular string and i want to know how many times that string occurs in whole file (5 Replies)
Hi all,
I need to search the database log find out the most frequently used tables for a certain period of time.
The search pattern is : the database.table
so, i need to look for ABCD.* in the entire log and then need the top ten tables.
I thought of using awk, search for the pattern ... (7 Replies)
I need to count the number of occurrences of a pattern, say 'key', between each occurrence of a different pattern, say 'lu'.
Here's a portion of the text I'm trying to parse:
lu S1234L_149_m1_vg.6, part-att 1, vdp-att 1 p-reserver IID 0xdb
registrations:
key 4156 4353 0000 0000
... (3 Replies)
i want to search a word in a file and find the count of occurences even if pattern occures twice in a same line.
for example file has the following content.
yes no no nooo yees
no yes
if I search for "no" it should give count as 4
Pls help.
Thanks (9 Replies)