Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6827925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:19:22+00:00 2026-05-26T22:19:22+00:00

The behaviour I want to replicate is like grep with -A and -B flags

  • 0

The behaviour I want to replicate is like grep with -A and -B flags .
eg grep -A 2 -B 2 "hello" myfile.txt will give me all the lines which have “hello” in them, but also 2 lines before and 2 lines after it.
Lets assume this table schema :

+--------+-------------------------+
| id     |    message              |
+--------+-------------------------+
| 1      | One tow three           |
| 2      | No error in this        |
| 3      | My testing message      |
| 4      | php module test         |
| 5      | hello world             |
| 6      | team spirit             |
| 7      | puzzle game             |
| 8      | social game             |
| 9      | stackoverflow           |
|10      | stackexchange           |
+------------+---------------------+

Now a query like :
Select * from theTable where message like '%hello%' will result in :

5 | hello world

How can I put another parameter “N” which selects N rows before, and N rows after the matched record i.e. for N = 2, the result should be :

    | 3      | My testing message      |
    | 4      | php module test         |
    | 5      | hello world             |
    | 6      | team spirit             |
    | 7      | puzzle game             |
  • For simplicity assume ‘like %TERM%’ matches only 1 row .
  • Here the result is supposed to be sorted on auto-increment id field.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:19:23+00:00Added an answer on May 26, 2026 at 10:19 pm

    Right, this works for me:

    SELECT child.*
    FROM stack as child,
    (SELECT idstack FROM stack WHERE message LIKE '%hello%') as parent
    WHERE child.idstack BETWEEN parent.idstack-2 AND parent.idstack+2;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to implement file uploading with behaviour which is commonly seen in various
I want to be able to replicate this adsutil.vbs behaviour in PowerShell: cscript adsutil.vbs
I'm writing a Ruby script and want behaviour like this: every 15 seconds do
I have a scrollview with all my data to be showed and I want
I want to change the behaviour of the editable ComboBox. This is the behaviour
Lets say I want to make few classes to determine behaviour of agents. The
I found strange generics behaviour. In two words - thing I realy want is
In following code, I want to extend the behaviour of a class by deriving/subclassing
What I want to do is provide some public static fields that will be
We want to have two .NET apps running on the same machine communicate with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.