Pattern count in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pattern count in file
# 1  
Old 06-03-2016
Pattern count in file

hi ,

I have a below file which contain as

Code:
Use descriptive thread titles when posting Urgent. 

For example, do not post  questions with subjects like "Help Me!", "Urgent Urgent Urgent" .

or "Doubt". Post  subjects like "Execution Problems with Cron" or "Help with Backup Shell  Script". See new thread posting rules at the top of this page. 

Thanks!



I want the keyword Urgent pattern count in each line in my file

Last edited by Don Cragun; 06-06-2016 at 04:21 AM.. Reason: Change PHP tags to CODE tags.
# 2  
Old 06-03-2016
Can you give 1-2 more example files, describe exactly what the output should be and enclose the output as you want it to be for the different example files?
This User Gave Thanks to stomp For This Post:
# 3  
Old 06-03-2016
The following prefixes each line with the number of matches
Code:
 awk '{x=$0; print gsub("\<Urgent\>","",x),$0}' file

# 4  
Old 06-06-2016
my output would be like below..


Code:
Line :1
Line 2: 3
line 3 :0
Line 4 : 0

Moderator's Comments:
Mod Comment Please use CODE (not PHP tags) when displaying sample input, sample output, and code segments.

Last edited by Don Cragun; 06-06-2016 at 04:25 AM.. Reason: ADD CODE tags again!
# 5  
Old 06-06-2016
Quote:
Originally Posted by Jewel
my output would be like below..


Code:
Line :1
Line 2: 3
line 3 :0
Line 4 : 0

Moderator's Comments:
Mod Comment Please use CODE (not PHP tags) when displaying sample input, sample output, and code segments.
You have shown us a seven line sample input file and you have shown us a sample output file with four lines. If you wanted the code to ignore empty lines, or to ignore blank lines, or to ignore lines with no occurrences of your keyword until you have found two lines containing your key word and then print no more than two lines containing zero occurrences of your key word; don't you think think you should explain what criteria should be used to determine which lines are to be processed or counted or numbered???

Why isn't the line number supposed to be included in the 1st line of output? Why is there a space before the colon sometimes and a space after the colon sometimes in the output.
# 6  
Old 06-06-2016
Please, give it a try:
Code:
perl -nle  '$c=()=$_=~/Urgent/g; printf "Line $.: %d\n", $c' Jewel.file

Output:
Code:
Line 1: 1
Line 2: 3
Line 3: 0
Line 4: 0

# 7  
Old 06-06-2016
Code:
awk 'NF {print "Line " ++l ": " gsub("Urgent", "&")}' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep pattern file and count occurances

Guys I am trying grep to read from pattern file and count occurances of each word. input is firstplace secondplace secondpot patternfile is place first second i want the following. 1-count number of times keywords in patternfile occurs in begining of each line in input file. so... (12 Replies)
Discussion started by: ahfze
12 Replies

2. 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

3. UNIX for Dummies Questions & Answers

How to find count total number of pattern in a file …?

How to find count total number of pattern in a file … File contains : a.txt ------------- aaa bbb nnn ccc aaa bbb aaa ddd aaa aaa aaa aaa grep -c aaa a.txt Op: 4 ( But my requirement is should count the total no of patterns as 7 ) (4 Replies)
Discussion started by: Jitten
4 Replies

4. UNIX for Advanced & Expert Users

Count number of lines between a pattern in a large file

1000CUS E Y4NYRETAIL 10010004HELIOPOLIS 110000500022360591000056XX EG 1101DEBY XXAD ZSSKY TSSROS 1102HANYNNYY@HOTMAIL.COM 210030/05/201301/06/2013AED 3100 OPE 3100 CLO 3100 The 1000CUS E Y NYCORPORATE 10010004HELIOPOLIS 110000500025270504550203XX EG 1101XXXQ FOR CTING AND... (1 Reply)
Discussion started by: john2022
1 Replies

5. Shell Programming and Scripting

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- 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)
Discussion started by: swayam123
4 Replies

6. Shell Programming and Scripting

Checking a pattern in file and the count of characters

I am having a zipped file which has the following URL contents - 98.70.217.222 - - "GET /liveupdate-aka.symantec.com/1340071490jtun_nav2k8enn09m25.m25?h=abcdefgh HTTP/1.1" 200 159229484 "-" "hBU1OhDsPXknMepDBJNScBj4BQcmUz5TwAAAAA" "-" In this line here is we only need to consider the... (4 Replies)
Discussion started by: Naks_Sh10
4 Replies

7. Shell Programming and Scripting

Count of matched pattern occurences by minute and date in a log file

Anyone knows how to use AWK to achieve the following Sun Feb 12 00:41:01-00:41:59 Success:2 Fail:2 Sun Feb 12 00:42:01-00:42:59 Success:1 Fail:2 Sun Feb 12 01:20:01-01:20:59 Success:1 Fail:2 Mon Feb 13 22:41:01-22:41:59 Success:1 Fail:1 log file: Success Success Fail Fail ... (9 Replies)
Discussion started by: timmywong
9 Replies

8. Shell Programming and Scripting

Count of matched pattern occurences by time in a log file

We have a log file, the format is similar to this: 08/04/2011 05:03:08 Connection Success 08/04/2011 05:13:18 Connection Success 08/04/2011 05:23:28 Connection Fail 08/04/2011 05:33:38 Connection Success 08/04/2011 06:14:18 Connection Success 08/04/2011 06:24:28 Connection Fail 08/04/2011... (6 Replies)
Discussion started by: clu
6 Replies

9. Shell Programming and Scripting

How to count the pattern in a file by awk

hello everybody, I have 3 files eg- sample1 sample2 sample3 each file contain word babu many times eg- cat sample1 babu amit msdfmdfkl babu abhi babu ruby amit babu I want to count only the count of babu ,how many times it appeared . (5 Replies)
Discussion started by: abhigrkist
5 Replies

10. 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
Login or Register to Ask a Question