Unix script to search within a .tar or .gz file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix script to search within a .tar or .gz file
# 1  
Old 11-11-2010
Question Unix script to search within a .tar or .gz file

HI,

I want to get few line from the file which is in .gz file. .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 current line(where search string present).
I tried the zgrep and got the line number, but when i use head or Tail command, Its giving some garbage values. I think we cannot use the Head or tail commands with the zip files containing multiple files.
Please suggest if there is any simple way?
# 2  
Old 11-11-2010
try these options if you have a GNU system.
Code:
   Context Line Control
       -A NUM, --after-context=NUM
              Print NUM  lines  of  trailing  context  after  matching  lines.
              Places   a  line  containing  a  group  separator  (--)  between
              contiguous groups of matches.  With the  -o  or  --only-matching
              option, this has no effect and a warning is given.

       -B NUM, --before-context=NUM
              Print  NUM  lines  of  leading  context  before  matching lines.
              Places  a  line  containing  a  group  separator  (--)   between
              contiguous  groups  of  matches.  With the -o or --only-matching
              option, this has no effect and a warning is given.

# 3  
Old 11-11-2010
No .. I dont have GNU system and i cannot extract files, as they are very huge and takes lot of space
# 4  
Old 11-11-2010
awk or sed should work. there are plenty of examples out there simliar to what your doing.
# 5  
Old 11-11-2010
Code:
gzcat compress.tar.gz | nawk '{ a=b; b=c; c=$0 } c~"your PATTERN" {print a"\n"b"\n"c; exit}'

# 6  
Old 11-11-2010
the above command didn't work , so i modifed it to awk from nawk

Now I am getting error "awk: Input line "XXX" cannot be longer than 3,000 bytes."
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

UNIX command to check if file name ends with .tar OR if the file is a tar file

Hello Team, Would you please help me with a UNIX command that would check if file is a tar file. if we dont have that , can you help me with UNIX command that would check if file ends with .tar Thanks in advance. (10 Replies)
Discussion started by: sanjaydubey2006
10 Replies

2. Shell Programming and Scripting

UNIX shell script to search a string in a file

Hi folks, I am new for shell script, I hope somebody to help me to write shell script My requirement is below steps 1. I have apache access.log i.e located in /var/log/httpd/ Ex. 127.0.0.1 - - "GET... (14 Replies)
Discussion started by: Chenchireddy
14 Replies

3. UNIX for Dummies Questions & Answers

UNIX Tar file backup

I want to create a tar file that backup all my recent work. I have no idea how Tar file works and I am new to Unix Please help (1 Reply)
Discussion started by: GGBEASTBOI
1 Replies

4. UNIX and Linux Applications

Perl Script to read an excel file into an array and search in the UNIX directories

Hi, I want the Perl script with versions 5.8.2 and 5.8.5 starting with #!/usr/bin/perl The Perl program should read the excel file or text file line by line and taking into an array and search in the UNIX directories for reference file of .jsp or .js or .xsl with path .The Object names... (2 Replies)
Discussion started by: pasam
2 Replies

5. UNIX for Dummies Questions & Answers

search a word in a file present in tar file

i need to search a word in the tar file. if it is present just give me the file name please help me (1 Reply)
Discussion started by: junkbuster
1 Replies

6. UNIX for Dummies Questions & Answers

tar -cvf test.tar `find . -mtime -1 -type f` only tar 1 file

Hi all, 4 files are returned when i issue 'find . -mtime -1 -type f -ls'. ./ora_475244.aud ./ora_671958.aud ./ora_934052.aud ./ora_934050.aud However, when I issued the below command: tar -cvf test.tar `find . -mtime -1 -type f`, the tar file only contains the 1st file -... (2 Replies)
Discussion started by: ahSher
2 Replies

7. UNIX for Advanced & Expert Users

How to view .tar file in unix

Guys, Could anyone let me know, how we can see the contents of .tar file without extracting it. I know the extracting and combine options Regards (3 Replies)
Discussion started by: sdosanjh
3 Replies

8. UNIX for Advanced & Expert Users

How to create a Tar of multiple Files in Unix and FTP the tar to Windows.

Hi, On my Unix Server in my directory, I have 70 files distributed in the following directories (which have several other files too). These files include C Source Files, Shell Script Source Files, Binary Files, Object Files. a) /usr/users/oracle/bin b) /usr/users/oracle... (1 Reply)
Discussion started by: marconi
1 Replies

9. UNIX for Dummies Questions & Answers

tar file in unix

Hi all, Please give me one example session to create a tar file in unix Shruti (5 Replies)
Discussion started by: shruti_mgp
5 Replies

10. Shell Programming and Scripting

Creation and Transfer of TAR file from one machine to another Using UNIX script

Hi, I want to create unix script such that it should run on machine A, it should run TAR commands on machine B and copy that TAR to machine C. Is it possible? Thanks Rahul (2 Replies)
Discussion started by: rahuljadhav
2 Replies
Login or Register to Ask a Question