View 5 lines after the last occurence of a string


 
Thread Tools Search this Thread
# 1  
View 5 lines after the last occurence of a string

Hi Everyone,

I am looking for the unix command by which I can view 5 lines after the last occurence of a string (including the last occurence)

Example : I have the following lines in a unix file. I have to look for the last occurence of the string "How are you" and then view the next 5 lines from there. (Looking to print lines 6 to 10) What would be the command?
Line 1: Hi
Line 2: How are you
Line 3: Very good
Line 4: How are you
Line 5: Welcome to Unix Forum
Line 6: How are you
Line 7: Learn Many things here
Line 8: Hi
Line 9: My Name is Kannan
Line 10: Very good
Line 11: Test
Line 12: Welcome to Unix Forum
Line 13: Good
Line 7: Learn Many things here

# 2  
What's your system?
# 3  
The system is Unix and I am accessing it using Putty
# 4  
"What system are you using?"
"UNIX"
"What model is your car?"
"Blue"

Those are both very vague answers.

If you don't know, try uname -a, it will print more details.

---------- Post updated at 12:45 PM ---------- Previous update was at 12:43 PM ----------

If your system, whatever it is, has bash or ksh, you can try this:

Code:
#!/bin/ksh

exec 5>/tmp/$$
while read LINE
do
        [[ "$LINE" == *"How are you"* ]] && exec 5>/tmp/$$
        echo "$LINE" >&5
done

head -n 6 /tmp/$$
exec 5>&-
rm /tmp/$$

This User Gave Thanks to Corona688 For This Post:
# 5  
I am sorry for the wrong reply. I dont know these jargans Smilie.

uname -a returns this:-
Linux hn731c2n8 2.6.9-89.0.2.EL.msdw.1largesmp #1 SMP Fri Jun 12 15:15:13 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux

And I have ksh and bash in my system.

Thanks for the answer. It looks like a shell script. Is there a command as simple as a grep command to return the result that I am looking for?
# 6  
Code:
grep -A 5 'How are you' FILENAME | tail -6

Replace FILENAME with your filename. I hope you have the -A option in grep !
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Science: Computers
Difficulty: Medium
Linus Sebastian is the creator of the Linux kernel, which went on to be used in Linux, Android, and Chrome OS.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete after nth occurence of string in each line

Hello, Environment: I am under Ubuntu 18.04 bionic. I have an sql file consisting of 10K lines. Objective: What I am trying to attain is to remove everything coming after 2nd tab in each line. While searching for the answer, I found two answers and both gave expected result just for the first... (2 Replies)
Discussion started by: baris35
2 Replies

2. Shell Programming and Scripting

How to get lines with only one occurence of pattern?

My data 20161220 20:30:01 MODE 1 TEST 1 SOURCE 1 SET 1 20161220 20:30:02 MODE 1 TEST 2 SOURCE 1 SET 1 20161220 20:30:02 MODE 1 TEST 3 SOURCE 1 SET 1 20161220 20:30:02 MODE 1 TEST 1 SOURCE 2 SET 1 20161220 20:30:04 MODE 1 TEST 1 SOURCE 1 SET 1 MODE 1 TEST 2 SOURCE 2 SET 1 20161220 20:30:02... (7 Replies)
Discussion started by: migurus
7 Replies

3. Shell Programming and Scripting

Get nth occurence of string from a file

I have file in which the data looks like this, 01,0000000,xxxxxxx/ 02,xxxxxxxx,yyyyyy/ 03,test1,41203016,,/ 01,0000000,xxxxxxx/ 02,xxxxxxxx,yyyyyy/ ... (16 Replies)
Discussion started by: r@v!7*7@
16 Replies

4. Shell Programming and Scripting

Find the occurence of particular string in log file

I have a log file which looks like this: <845185415165:STATUS:5/0:0:0:0:0|ghy59DI5zasldf87asdfamas8df9asd903tGUVSQx4GJVSQ==> I have to extract DATE and number of times the keyword STATUS is shown on each date. Input is : <1354625655744:STATUS:5/0:0:0:0:0|ghy59DI5ztGUVSQx4GJVSQ==>... (8 Replies)
Discussion started by: maddyrox
8 Replies

5. UNIX for Dummies Questions & Answers

View 10 lines after the 1st occurence of a string

Hi Everyone, I am looking for the unix command by which I can view 10 lines after the 1st occurence of a string (including the 1st occurence) Example : I have the following lines in a unix file. I have to look for the 1st occurence of the string "How are you" and then view the next 10 lines... (3 Replies)
Discussion started by: kannanfile
3 Replies

6. UNIX for Dummies Questions & Answers

To find the Nth Occurence of Search String

Hi guys, I like to find the Line number of Nth Occurence of a Search string in a file. If possible, if it will land the cursor to that particualar line will be great. Cheers!! (3 Replies)
Discussion started by: mac4rfree
3 Replies

7. Shell Programming and Scripting

Extract a string up to the first occurence of a substring

Hi, I'm a newbie to shell scripting and have searched the forum but couldn't find what i was looking for. Basically I have a list of filenames like... 123-fileone.txt I want to be able to extract the prefix up to the first '-'. So I'd end up with 123. I have attempted it using a pretty... (2 Replies)
Discussion started by: kirkg
2 Replies

8. Shell Programming and Scripting

How to find vowel's occurence in a string

Hi All, I want to search the string for vowel's occurence and find the no of occurence of each vowels, Could anyone help me out? This is urgent to me...I m new to Shell programming.. Thanks and Regards, Nids:b: (4 Replies)
Discussion started by: Nidhi2177
4 Replies

9. Shell Programming and Scripting

Greping a string for only one occurence

Hi, I have a csv ( comma seperated value) file and I want to search for a particular string in each line of that file only if it occurs only once in the line. That is same string may be present more than once in a line but I want it to be greped only when it occurs just once. Please advice... (17 Replies)
Discussion started by: Piscian
17 Replies

10. Shell Programming and Scripting

Replace string B depending on occurence of string A

Depending upon the occurence of string 'xyz', I want to remove -t from the input file. There is not a fixed length input file. Any suggestions Input file: this is xyz line -t of the data this is line 2 of -t of the data xyz this is line 3 of -t the file this is line xyz of the -t file... (1 Reply)
Discussion started by: hemangjani
1 Replies

Featured Tech Videos