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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:27:13+00:00 2026-05-27T06:27:13+00:00

I need to extract a line above my search string (say, 19 lines above).

  • 0

I need to extract a line above my search string (say, 19 lines above). Normally, I would just go with

grep -B 19 $search_string $file | ...further processing

However the script should also work with on Solaris, where grep doesn’t provide the -B option. Often, I can get away with awk '/begin/,/end/' to print a bunch of lines, if I know the preceding lines. In this particular situation, this is not possible. I tried the following:

1) Ring buffer solution.

#!/bin/bash
g_a_buffer=( 0 )
g_i_buffer_index=1
while read line
        do
        g_a_buffer[$((g_i_buffer_index % 20))]=$line
        echo $line|grep $search_string > /dev/null
        [ $? -eq 0 ] && echo ${g_a_buffer[$(( (g_i_buffer_index + 2) % 20))]}
        let "g_i_buffer_index += 1"
        done < $file_name

This is extremely slow. For ~40k lines it takes 1m37s (against 0.005s for grep)

2) Awk solution. I have to say outright that I am an extrem beginner in awk, rarely going beyond awk ‘{print $1}’. The below line doesn’t work, but gives you an idea of what I am trying to achieve:

awk '/mySearchString/ {print NR-19}' filename.txt 

0.118s for execution, the speed is good! But all I get is a line number – 19. What I need is a printout of the line located at (line – 19). After some googling I still couldn’t find an answer. I admit this must be an extremely basic problem, but I seem to have hit a wall here.

All I found so far is how to print a previous line with awk (which is a sort of 1 line buffer), or massive implementations with ring buffer but in awk. Is there a more elegant way to do this?

Thanks for help!

  • 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-27T06:27:14+00:00Added an answer on May 27, 2026 at 6:27 am

    Here is a solution which requires two passes through the file so is not optimal, but may well perform reasonably in practice. (Tested on GNU awk, but no obvious reason why it would not work on Solaris).

    awk "$(awk '/mySearchString/ { print "NR==" NR-19 }' myInputFile.txt)" myInputFile.txt
    

    As this requires two passes, if you are piping the input from elsewhere you will need to store it in a temporary file somewhere.

    Alternatively if you know that your search string will appear at most once in the file (or at least you only care about the first occurrence), you could combine awk with head and tail to extract the line:

    awk 'NR==1,/mySearchString/' | tail -n 19 | head -n 1
    

    I don’t have a suitable text file handy to benchmark this, but I would expect it to be a fair amount better than your ring buffer solution.

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

Sidebar

Related Questions

I need to extract the zipcode from file's line. each line contains an adress
I am reading a file by line and need to extract latitude and longitude
I need to extract only the last row from a multi-line string using regular
I need to extract multiple matches for a string on a single line. The
I need to extract a set number of lines from a file given the
I need to extract this text: Line 1 text. Line 2 text. Line 2
Need to extract .co.uk urls from a file with lots of entries, some .com
I need to extract the arguments from a function Say, Function_1(arg1,agr2,....argn). Is it possible
I have the following string and need to extract the text inside the div's
I have a project where I need to extract a file from a zip,

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.