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 7865789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:11:33+00:00 2026-06-03T00:11:33+00:00

I have two values usb 1-3 and address 20. Now I am trying to

  • 0

I have two values “usb 1-3” and “address 20”. Now I am trying to search a text file ( basically a log file) which has both of these words.

Now that file has many lines with these words as:

Apr 27 13:30:55 box2 kernel: usb 1-3: USB disconnect, address 20
and
Apr 27 13:25:43 box2 kernel: usb 1-3: new high speed USB device using ehci_hcd and address 20

Now my requirement is that: I want to get the line with the second-to-last occurrence of these two words and then next 16 lines after that. Like if there are 6 occurrences in file (6 lines with both of these words) then I need the line with the 5th occurrence and the next 16 lines after it. If there are 10 occurrences then I need the 9th occurrence and the next 16 lines.

currently I was using

egrep -A 20 'usb 1-3:.*address 20' filename | tail -16 > output.

but it gives me last occurrence. not second-to-last. please note that line should have Both Words and in same order (usb 1-3 first and address 20 later)
Please tell me if my question isn’t clear. Thanks in advance.

  • 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-06-03T00:11:36+00:00Added an answer on June 3, 2026 at 12:11 am

    This requires two passes, but it should do the trick:

    tail -n +`egrep -n 'usb 1-3:.*address 20' filename |
      tac | sed -n '2s/:.*//p'` filename | head -17
    

    You could use awk or perl to do it in one pass, but you’d need to manage some buffers. Note that it uses tac, which is a reverse cat. If you don’t have it on your system, try to drop tail -r in its place.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I have two text inputs which get their values from a datepicker. What
Dictionary cannot have two values with same key. please tell which logic/algorithm being used
I have two integer values, x and total. I am trying to find the
I have two arrays with time values in them in this format. 00:00:00 which
I have two or more tables which hold values under the same column name.
I have two lists from which I have to get two values(MyCaption and MyValue).
I have two columns with two values.. I want to append some text to
i am using fusion charts with php. Now i have two values of blood
I have two float values, 'a' and 'b' . I need to calculate the
I have two arrays of values like X,Y,Z and 1,2 There is a table

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.