Sponsored Content
Top Forums Shell Programming and Scripting How to strictly grep (with before and after args) for last N number of occurrences? Post 303021692 by samjna on Tuesday 14th of August 2018 08:20:52 PM
Old 08-14-2018
How to strictly grep (with before and after args) for last N number of occurrences?

Here is my grep string to print only the last occurrence, with before and after lines. Note that the tail Argument is sum of B and A args + 1, so that it prints the data of only the last 1 match. Now I need to print last 2 such matches. I thought of doubling the tail arg like 5+5+1 (For -- line), for ex: tail -11 should return last 2 matches, but I want it to be strict (I guess its tail -maxNumber, but I want it to be something like tail -MinNumber), so it has to either print 2 occurrences or nothing. Right now even if there is only one sample, it returns the output, I want output only if there are at least 2 matches. May be we can pipe it to awk to solve this. Please help.

Code:
grep -h -B 1 -A 3 'Authentication Error' MyLogFile.txt | tail -5

Output is:

Code:
	line1 Content
	Authentication Error while calling
	line3 Content
	line4 Content
	line5 Content

If there are multiple occurrences:

Code:
grep -h -B 1 -A 3 'Authentication Error' MyLogFile.txt | tail -11

Output is:

Code:
	line1 Content
	Authentication Error while calling
	line3 Content
	line4 Content
	line5 Content
--	  
	line11 Content
	Authentication Error while calling
	line13 Content
	line14 Content
	line15 Content

But even if there is only one occurrence:

Code:
grep -h -B 1 -A 3 'Authentication Error' MyLogFile.txt | tail -11

Output is:

Code:
	line1 Content
	Authentication Error while calling
	line3 Content
	line4 Content
	line5 Content


Last edited by samjna; 08-14-2018 at 10:26 PM..
 

10 More Discussions You Might Find Interesting

1. Programming

output the letters of the alphabet with the number of occurrences

hi, I'm trying to create a program that will read a file and then check the file for each letter of the alphabet and then output the letter and the number of times it appears in the file, into a new file... this is what i have so far but it's not working.. if anyone could help that would be nice!... (10 Replies)
Discussion started by: svd
10 Replies

2. Shell Programming and Scripting

Count the number of occurrences of the word

I am a newbie in UNIX shell script and seeking help on this UNIX function. Please give me a hand. Thanks. I have a large file. Named as 'MyFile'. It was tab-delmited. I am told to write a shell function that counts the number of occurrences of the ord “mysring” in the file 'MyFile'. (1 Reply)
Discussion started by: duke0001
1 Replies

3. UNIX for Dummies Questions & Answers

Print number of occurrences of many different strings

People, I need your help with making a script which will 1. take as an input the number of lines, smth like this: ((RUBROBACTER_1_PE1288 (((SALINISPORA_1_PE1863 SALINISPORA_1_PE1828)100 ((NOCARDIOIDES_2_PE2419 PROPIONIBACTERIUM_1_PE1395)96 ((((((((CORYNEBACTERIUM_1_PE1119... (3 Replies)
Discussion started by: roussine
3 Replies

4. Shell Programming and Scripting

counting number of pattern occurrences

Hi All, Is it possible to count number of occurrences of a pattern in a single record using awk?? for example: a line like this: abrsjdfhafa I want to count the number of a character occurrences. but still use the default RS, I don't want to set RS to single character. (1 Reply)
Discussion started by: ghoda2_10
1 Replies

5. Linux

How to sort the number of occurrences

file:///C:/Users/TSHEPI%7E1.LEB/AppData/Local/Temp/moz-screenshot.pngATM@ubuntu:~$ cat numbers2 | sort -n | uniq -c 1 7 1 11 2 10 3 the 1st numbers are the counts from the command "uniq -c", which represent the number of occurrences of each in the file. The "sort -n"... (4 Replies)
Discussion started by: lebogot
4 Replies

6. Shell Programming and Scripting

Help with Unix and Awk to count number of occurrences

Hi, I have a file (movies.sh), this file contains list of movies such as I want to redirect the movies from movies.sh to file_to_process to allow me process the file with out losing anything. I have tried Movies.sh >> file_to_process But I want to add the row number to the data... (2 Replies)
Discussion started by: INHF
2 Replies

7. Shell Programming and Scripting

How to search number of occurrences of a particular string in a file through vi editor?

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)
Discussion started by: sheelsadan
5 Replies

8. Shell Programming and Scripting

Script to Serach pattern and give number of occurrences

Hi, I want a script which search for a pattern "good" in a huge file and provide me number of occurences of such pattern in a file. lets say i have a file test.txt contents as below good is good but good is sometime bad and sometime good you are very good and good is always good ... (7 Replies)
Discussion started by: sv0081493
7 Replies

9. Shell Programming and Scripting

GREP between last occurrences of two strings

Hi I have the server.log file as: Server Stopped ABC DEF GHI JKL Server Started MNO PQR STU Server Stopped VWX YZ ABC Server Started Server Stopped 123 456 789 (9 Replies)
Discussion started by: ankur328
9 Replies

10. Shell Programming and Scripting

Inconsistent `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l`

i have this line of code that looks for the same file if it is currently running and returns the count. `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l` basically it is assigned to a variable ISRUNNING=`ps -eaf -o args | grep -i sfs_pcard_load_file.ksh |... (6 Replies)
Discussion started by: wtolentino
6 Replies
MB_OUTPUT_HANDLER(3)							 1						      MB_OUTPUT_HANDLER(3)

mb_output_handler - Callback function converts character encoding in output buffer

SYNOPSIS
string mb_output_handler (string $contents, int $status) DESCRIPTION
mb_output_handler(3) is ob_start(3) callback function. mb_output_handler(3) converts characters in the output buffer from internal charac- ter encoding to HTTP output character encoding. PARAMETERS
o $contents - The contents of the output buffer. o $status - The status of the output buffer. RETURN VALUES
The converted string. CHANGELOG
+--------+---------------------------------------------------+ |Version | | | | | | | Description | | | | +--------+---------------------------------------------------+ | 4.1.0 | | | | | | | This handler now adds the charset HTTP header | | | when the following conditions are met: | | | | | | o Content-Type has not been set, | | | using header(3). | | | | | | o The default MIME type begins with | | | text/. | | | | | | o The mbstring.http_input setting | | | is something other than | | | pass. | | | | +--------+---------------------------------------------------+ EXAMPLES
Example #1 mb_output_handler(3) example <?php mb_http_output("UTF-8"); ob_start("mb_output_handler"); ?> NOTES
Note If you want to output binary data, such as an image, using PHP 4.3.0 or later, a Content-Type: header must be set using header(3) before any binary data is sent to the client (e.g. header("Content-Type: image/png")). If Content-Type: header is sent, output char- acter encoding conversion will not be performed. Note that if 'Content-Type: text/*' is sent, the content body is regarded as text; conversion will take place. If you want to output binary data, such as an image, using PHP 4.2.x or earlier, you must set output encoding to "pass" using mb_http_output(3). SEE ALSO
ob_start(3). PHP Documentation Group MB_OUTPUT_HANDLER(3)
All times are GMT -4. The time now is 03:13 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy