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

  • Home
  • SEARCH
  • 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 3392344
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:54:35+00:00 2026-05-18T03:54:35+00:00

I want to get a few lines from a file which is in a

  • 0

I want to get a few lines from a file which is in a compressed .gz file.

The .gz file contains many txt files and I want to search a string in all these txt files and need to get the previous 3 line as output, including the current line (where the search string is present).

I tried zgrep and got the line number, but when I use head or tail command It’s giving some garbage values. I think we cannot use the head or tail commands with compressed files containing multiple files.

Please suggest if there is any simple way?

  • 1 1 Answer
  • 1 View
  • 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-18T03:54:36+00:00Added an answer on May 18, 2026 at 3:54 am

    The essence of how to accomplish this is to get the names of the files within the tarball to search over, and extract their content to be searched, while not extracting anything else. Because we don’t want to write to the file system, we can use the -O flag to instead extract to standard-out.

    tar -tzf file.tar.gz | grep '\.txt' | xargs tar -Oxzf file.tar.gz | grep -B 3 "string-or-regex" will concatenate all of the files in the .tar.gz with names ending in “.txt”, and grep them for the given string, also outputting the 3 previous lines. It won’t tell you which file in the tarball any match came from, and the “three previous lines” may in fact come from the previous file.

    You can instead do:

    for file in $(tar -tzf file.tar.gz | grep '\.txt'); do 
        tar -Oxzf file.tar.gz "$file" | grep -B 3 --label="$file" -H "string-or-regex"
    done
    

    which will respect file boundaries, and report the file names, but be much less efficient.

    (-z tells tar it is gzip compressed. -t lists the contents. -x extracts. -O redirects to standard output rather than the file system. Older tars may not have the -O or -z flag, and will want the flags without -: e.g. tar tz file.tar.gz)

    Okay, so you have an unusable grep. We can fix that with awk!

    #!/usr/bin/awk -f
    BEGIN { context=3; }
    { add_buffer($0) }
    /pattern/ { print_buffer() }
    function add_buffer(line)
    {
        buffer[NR % context]=line
    }
    function print_buffer()
    {
        for(i = max(1, NR-context+1); i <= NR; i++) {
            print buffer[i % context]
        }
    }
    function max(a,b)
    {
        if (a > b) { return a } else { return b }
    }
    

    This will not coalesce adjacent matches, unlike grep -B, and can thus repeat lines that
    are within 3 lines of two different matches.

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

Sidebar

Related Questions

i want query a file xml with linq, i want get all descendat of
I want to get 100 and example from this string ?connect:100/username:example/ I searched in
Let's say file.txt.gz has 2GB, and I want to see last 100 lines or
I want to insert a few certain values from a php file, to my
I want get all of the Geom objects that are related to a certain
I want get as much as possible from Redis + Hiredis + libevent. I'm
What I want: get a xml from the AppData to use What I code
Basically I want get data I already have accessed from javascript and passing it
I want to get a list of files and then read the results into
A few days back I had to de-serialize data from xml file to my

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.