How to output all lines following Nth occurrence of string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to output all lines following Nth occurrence of string
# 1  
Old 02-28-2012
How to output all lines following Nth occurrence of string

Greetings experts. Searched the forums (perhaps not hard enough?) - Am searching for a method to capture all output from a log file following the nth occurrence of a known string.

Background:
Using bash, I want to monitor my Oracle DB alert log file. The script will count the total # of occurrences the string "ORA-" appears in alert_SID.log.

Code:
$ cat alert_SID.log | grep -v grep | grep "ORA-" | wc -l > curcnt.txt

Each time the script runs, it will get the current count of occurrences of the string "ORA-" and compare that count to that stored in the "curcnt.txt" text file. If the "newest" count is greater than the "current" count, then I would like to capture the alert log from the Nth occurrence (in this case, the value of "current") on and receive the contents of that file.

My thought process is that there should be a relatively straightforward way to parse my alert_SID.log up until the Nth occurrence and grab all output following.

Any constructive feedback and/or tips to get me pointed in the right direction is much appreciated.

Thanks,
CJ
# 2  
Old 02-28-2012
Code:
awk '/ORA-/{i++}i>N' alert_SID.log

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 02-28-2012
@bartus11

That appears to give me what I need. I appreciate the quick reply and minimal solution.

CJ
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trim after nth occurrence with loop

Hello, Below command trims right after the nth occurrence of a string. When I try in while loop, it is not working. In Terminal IFS=/ ; read -ra val < Textfile ; echo "${val:0:3}" It gives only one line: sunday/monday/tuesday Textfile: sunday/monday/tuesday/wednesday/thursday... (2 Replies)
Discussion started by: baris35
2 Replies

2. Shell Programming and Scripting

How to insert file contents after nth occurrence of a string using sed?

Hi, I would like to know how, using sed, be able to insert contents of file2 in file1 after say the second occurrence of a given string? e.g. > cat file1 banana apple orange apple banana pear tangerine apple > cat file2 I don't like apples What would be the sed command to insert... (5 Replies)
Discussion started by: dimocn
5 Replies

3. Shell Programming and Scripting

Extract the text between the nth occurrence of square brackets

Please can someone help with this? I have a file with lines as follows: word1 word2 word3 word4 word5 word6 word7 word8 word1 word2 word3 word4 word5 word6 word7 word8 word1 word2 word3 word4 word5 word6 word7 word8 word1 word2 word3 word4 word5 word6 word7 word8 When I use the... (7 Replies)
Discussion started by: Subhadeep_Sahu
7 Replies

4. Shell Programming and Scripting

Insert new pattern in newline after the nth occurrence of a line pattern - Bash in Ubuntu 12.04

Hi, I am getting crazy after days on looking at it: Bash in Ubuntu 12.04.1 I want to do this: pattern="system /path1/file1 file1" new_pattern=" data /path2/file2 file2" file to edit: data.db - I need to search in the file data.db for the nth occurrence of pattern - pattern must... (14 Replies)
Discussion started by: Phil3759
14 Replies

5. Shell Programming and Scripting

Extracting lines after nth LINE from an output

Hi all, Here is my problem for which i am breaking my head for past three days.. I have parted command output as follows.. Model: ATA WDC WD5000AAKS-0 (scsi) Disk /dev/sdb: 500GB Sector size (logical/physical): 512B/512B Partition Table: msdos Number Start End Size Type ... (3 Replies)
Discussion started by: selvarajvs
3 Replies

6. Shell Programming and Scripting

Replacing nth occurrence

There is already one thread with the same heading. But here the case is little different. i have a line which have a field separator '|' abc|def|ghi|jkl|mno|pqr|stu|vwx|yz I want to replace every 3rd occurance + next character with the same + newline character.. I mean i want to enter a... (6 Replies)
Discussion started by: ratheeshjulk
6 Replies

7. Shell Programming and Scripting

find string nth occurrence in file and print line number

Hi I have requirement to find nth occurrence in a file and capture data from with in lines (between lines) Data in File. <QUOTE> <SESSION> <ATTRIBUTE NAME='Parameter Filename' VALUE='file1.parm'/> <ATTRIBUTE NAME='Service Name' VALUE='None'/> </SESSION> <SESSION> <ATTRIBUTE... (6 Replies)
Discussion started by: tmalik79
6 Replies

8. Shell Programming and Scripting

remove characters from string based on occurrence of a string

Hello Folks.. I need your help .. here the example of my problem..i know its easy..i don't all the commands in unix to do this especiallly sed...here my string.. dwc2_dfg_ajja_dfhhj_vw_dec2_dfgh_dwq desired output is.. dwc2_dfg_ajja_dfhhj it's a simple task with tail... (5 Replies)
Discussion started by: victor369
5 Replies

9. Shell Programming and Scripting

extract nth line of all files and print in output file on separate lines.

Hello UNIX experts, I have 124 text files in a directory. I want to extract the 45678th line of all the files sequentialy by file names. The extracted lines should be printed in the output file on seperate lines. e.g. The input Files are one.txt, two.txt, three.txt, four.txt The cat of four... (1 Reply)
Discussion started by: yogeshkumkar
1 Replies

10. UNIX for Dummies Questions & Answers

Finding nth occurrence in line and replacing it

Hi, I have several files with data that have to be imported to a database. These files contain records with separator characters. Some records are corrupt (2 separators are missing) and I need to correct them prior to importing them into the db. Example: ... (5 Replies)
Discussion started by: stresing
5 Replies
Login or Register to Ask a Question