This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Linux Grep Commands For TAR file to identify a pattern  RSS feed

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

For Zip files *.gz, We are using zgrep command to search for a pattern in zip files.

I have created these two zip files one.gz, two.gz

Created a Tar TestTar.tar with the above two zip files


[b]How to find a pattern in a tar file without extracting the above tar file.


do we have any commands?[b]
 
lowercase baba
Posts: 12746
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
without extracting at all?  I think you have to extract them to some extent, but you don't have to extract them to disk.  The -O flag should extract them to stdout, which you could then pipe to your zgrep...so something like


tar xvf <your.tar.file> -O | zgrep <yourpattern>
 
Bartender
Posts: 20766
124
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A ZIP file as Windows people know them is both an archive (bundle of files/directories contained within a single file) and a compressed file. That includes the Java jar utility, since a JAR is just a ZIP archive with an extra file or so of meta-data in the archive.

To produce a "zipped tar", though, the process is to first archive everything with tar*, then gzip the archive file which also known as a "tarball". Unlike zip, gzip does not archive, it just crunches a single file.

The convention for such compressed archives is to suffix with with either ".tar.gz" or ".tgz". The gzip utility by default will take a file "f", crunch it down, and replace is with "f.gz"

About the time Linux first began to catch on, some genius realized that they could save some typing by making tar run the gzip (or some other selected) utility against the archive it was working with. That's the "z" option now widely seen in tar commands.

ZIP-style compression distorts the data so much that there's absolutely no way that grep can pattern-match against a gzipped file (whether it's tar'ed or not). Although I think that there is a form of grep that can uncompress on the fly to do comparisons. If not, that's why pipelining is so popular in the Unix world.

Note that the zip utility by default actually strives to obtain maximum compression, but not every compression algorithm is optimal for every file. In fact, choose the wrong algorithm and the "compressed" file can end up larger than the original one! So zip has 4 or 5 different algorithms and it will try them all. You can see this at work if you watch the output from ZIP, since it names which algorithm it decided on as it processes each file. If a file is listed simply as "stored", that means that the best compression for it was no compression at all.

So you can see that zip optimizes compression of each file in a ZIP archive, but tar/zip optimizes compression of the archive as a unit.

===
*  For those who never worked with old time mainframes and minicomputers, "tar" means "tape archive" from back when most data storage was on reels of magnetic tape.
 
He's giving us the slip! Quick! Grab this tiny ad!
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!