Monday 27 August 2012

How to read a .gz file, one line at a time and Power of Z command ?

#gzcat test1.gz

#gzcat my_tar_archive.tgz | tar -tvf -


[Solved] Read a .gz file line by line without using gzcat

Hi all
Is there a way to read and process a gzip file line by line similar to a text file without using gzcat..

while processing a text file we will usually use the logic 

Code:
exec<sample.txt
while read line
do 
echo $line
done

Since gzip, gunzip and gzcat are the same executable, so if you have one, you have all, available all over as a binary or source. Normally, you do something like this:

gunzip < file.gz | while read ln
do
. . .
done
Compatability: uncompress will not work on .gz, but gunzip, gzcat work on compress output (.Z).


=================================================

The Power of Z Commands – Zcat, Zless, Zgrep, Zdiff Examples ?

You can do the following normal file operations on the compressed file
  1. Viewing the compressed file with zcat.
  2. Paging the compressed file with zless / zmore.
  3. Searching inside the compressed file with zgrep / zegrep.
  4. Comparison of file using zdiff / zcmp

The old way...

Let us say you have a file called data..txt.gz. To display file you can type:
gzip -d data.txt.gz
cat data.txt
less data.txt

The new way...

Just use zless command:
zless data.txt.gz
zmore data.txt.gz

zcat command

Concatenate compressed files and print on the screen:
zcat file.gz

zdiff / zcmp command

Compare compressed files:
zdiff file1.gz file2.gz
zcmp file1.gz file2.gz

zegrep / zfgrep / zgrep command

Search compressed files for a regular expression:
zegrep -w '^word1|word2' file.gz
zgrep 'word' file.gz

zless / zmore commands

zmore and zless is a filter which allows examination of compressed or plain text files one screenful at a time on a screen. zmore works on files compressed with compress, pack or gzip, and also on uncompressed files. If a file does not exist, zmore looks for a file of the same name with the addition of a .gz, .z or .Z suffix.
zmore file.gz
zless file.gz

znew command

Znew recompresses files from .Z (compress) format to .gz (gzip) format. If you want to recompress a file already in gzip format, rename the file to force a .Z extension then apply znew.
znew file.Z

zdump command

zdump command prints the current time in each zonename named on the command line. Let us say your current time zone is IST (Indian standard time) and like to see time current time for Los Angeles (USA - PDT), enter:
date
Output:
Fri Aug 31 20:51:39 IST 2007
Now display Los Angeles current time :
zdump /usr/share/zoneinfo/America/Los_Angeles
Output:
/usr/share/zoneinfo/America/Los_Angeles  Fri Aug 31 08:20:31 2007 PDT

zipgrep command

Search files in a ZIP archive for lines matching a pattern:
zipgrep *.cpp basesys.zip
Example 1: View Compressed File and Uncompress with zcat
Compressing a file using gzip creates a compressed file with *.gz extension. You can view a compressed file with zcat with the following way. Which would be as same as the uncompressed file operation ‘cat filename’. zcat uncompresses the file and shows it in the stdout.
$ zcat filename.gz | more
$ ls -l big-file.*
-rw-r--r-- 1 ramesh ramesh 24853275 May  9 15:14 big-file.txt

$ gzip big-file.txt 
[Note: Compress the file]

$ ls -l big-file.*
-rw-r--r-- 1 ramesh ramesh 9275204 May  9 15:14 big-file.txt.gz

$ zcat big-file.txt.gz 
[Note: View the file without uncompressing it]

zcat big-file.txt.gz > big-file.txt
[Note: Uncompress the file]

Example 2: View a gzipped file which don’t have the gz suffix.

You can uncompress a gzipped file which don’t have the gz suffix. If you try to uncompress a gzipped file which don’t have the gz suffix with “gunzip” or “gzip -d” command you will face the following error.
gunzip: auth.log: unknown suffix -- ignored
But this zcat will uncompress the file and shows the content as shown below.
$ cat > test-file.txt
This is a test file used for gunzip and zcat testing

zcat is awesome command.  

$ gzip test-file.txt

$ mv test-file.txt.gz test-file-no-ext

$ gzip -d test-file-no-ext
gzip: test-file-no-ext: unknown suffix -- ignored

$ zcat test-file-no-ext
This is a test file used for gunzip and zcat testing

zcat is awesome command.

Example 3: Display the file content without worrying about whether it is compressed or not

When you are not sure whether a file is compressed or not, you can still view the file without worrying about it’s compression status as shown below.

In this example, If the input-file is compressed zcat will display the content by uncompressing it. If the input-file is not compressed zcat will display the content as it is.
$ zcat -f input-file

Example 4: Paging the compressed file with zless / zmore.

You can paginate a compressed file with zless command or zmore command as shown below.

$ zcat filename.gz | more
$ zcat filename.gz | less

(or)

$ zless filename.gz
$ zmore filename.gz
Example 5: Searching inside the compressed file with zgrep / zegrep.
You can search inside a compressed file with zgrep / zegrep as shown below. This would be as same as the uncompressed file operation ‘grep -i filename’. All the options to the zgrep command will be passed to grep, and the file will be fed to grep command. It may uncompress and feed the file to grep command if needed.
$ cat > test-file.txt
gzip, gunzip, zcat - compress or expand files
zless - file perusal filter for crt viewing of compressed text
zcmp, zdiff - compare compressed files

$ grep -i less test-file.txt
zless - file perusal filter for crt viewing of compressed text

$ gzip test-file.txt

$ zgrep -i less test-file.txt.gz
zless - file perusal filter for crt viewing of compressed text

Example 6: Comparison of file using zdiff / zcmp
You can compare two compressed files with zdiff / zcmp as shown below. This would be same as the uncompressed file operation ‘diff file1 file2′.
$ cat > file1.txt
This is line one
This is line two

$ cat > file2.txt
This is line 1
This is line two

$ diff file1.txt file2.txt
1c1
< This is line one
---
> This is line 1

$ gzip file1.txt file2.txt 

$ zdiff file1.txt.gz file2.txt.gz
1c1
< This is line one
---
> This is line 1

No comments:

Post a Comment