Difference between revisions of "Working with compressed files"

From LPTMS Wiki
Jump to: navigation, search
m
(Undo revision 339 by Roux (talk))
 
(3 intermediate revisions by the same user not shown)
Line 2: Line 2:
 
  zgrep, zcat, zdiff, zless, zmore, zegrep,...
 
  zgrep, zcat, zdiff, zless, zmore, zegrep,...
 
Here is a simple extension of ''tail'' and ''head'' for zipped files, that you can call ''ztail'' and ''zhead'' (example is for tail, replace "tail" with "head" everywhere to get zhead) and add to your own /bin directory:
 
Here is a simple extension of ''tail'' and ''head'' for zipped files, that you can call ''ztail'' and ''zhead'' (example is for tail, replace "tail" with "head" everywhere to get zhead) and add to your own /bin directory:
 +
 +
=====Caution:=====
 +
very basic version, works when the options starts with '-' so it works with '-n10' but fails with '-n 10'. Should be easily improved...
 +
 
<source lang="bash">
 
<source lang="bash">
#!/bin/bash
+
#!/bin/bash                                                                                                                                                
 +
 
 +
# This program is free software; you can redistribute it and/or modify                                                                                     
 +
# it under the terms of the GNU General Public License as published by                                                                                     
 +
# the Free Software Foundation; either version 2 of the License, or                                                                                         
 +
# (at your option) any later version.                                                                                                                       
 +
 
 +
# This program is distributed in the hope that it will be useful,                                                                                           
 +
# but WITHOUT ANY WARRANTY; without even the implied warranty of                                                                                           
 +
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                                                                             
 +
# GNU General Public License for more details.                                                                                                             
 +
 
 +
# You should have received a copy of the GNU General Public License along                                                                                   
 +
# with this program; if not, write to the Free Software Foundation, Inc.,                                                                                   
 +
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                                                                                               
 +
 
 +
# based on zless                                                                                                                                           
 +
 
 
PATH=${GZIP_BINDIR-'/bin'}:$PATH
 
PATH=${GZIP_BINDIR-'/bin'}:$PATH
  
usage="Usage: $0 [OPTIONS]... [FILES]...
+
version="ztail 0.1                                                                                                                                         
Like 'tail', but operate on the uncompressed contents of any compressed FILEs.
+
Copyright (C) 2007, Free Software Foundation, Inc. <http://fsf.org/>                                                                                       
 +
This is free software. You may redistribute copies of it under the terms of                                                                               
 +
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.                                                                                     
 +
There is NO WARRANTY, to the extent permitted by law.                                                                                                       
 +
                                                                                                                                                           
 +
Written by Guillaume Roux."
  
 +
usage="Usage: $0 [OPTIONS]... [FILES]...                                                                                                                   
 +
                                                                                                                                                           
 +
Same as the 'tail' command, but operate on the uncompressed contents of any compressed FILEs.                                                               
 
Options are the same as for 'tail'."
 
Options are the same as for 'tail'."
  
Line 14: Line 43:
 
--help)    exec echo "$usage";;
 
--help)    exec echo "$usage";;
 
-h)        exec echo "$usage";;
 
-h)        exec echo "$usage";;
 +
--version) exec echo "$version";;
 
esac
 
esac
  
 
options=""
 
options=""
files=""
+
count=0
 
for i in $@; do
 
for i in $@; do
 
str="$i"
 
str="$i"
 
if test -f $i && [ ${str:(-3)} = ".gz" ]; then
 
if test -f $i && [ ${str:(-3)} = ".gz" ]; then
     files=$files"$i "
+
     files[++count]=$str
 
else
 
else
 
     if [ ${str:0:1} = "-" ] && [ $str != "-v" ] && [ $str != "--verbose" ]; then
 
     if [ ${str:0:1} = "-" ] && [ $str != "-v" ] && [ $str != "--verbose" ]; then
        options=$options"$i "
+
options=$options"$i "
 
     elif [ $str != "-v" ] && [ $str != "--verbose" ]; then
 
     elif [ $str != "-v" ] && [ $str != "--verbose" ]; then
 
         echo -ne "***Warning: $i is neither a regular zip file or a regular option***\n\n"
 
         echo -ne "***Warning: $i is neither a regular zip file or a regular option***\n\n"
Line 31: Line 61:
 
done
 
done
  
for file in $files; do
+
if [ $count -gt 0 ]; then
    echo "==>"$file"<=="
+
    if [ $count -eq 1 ]; then
    exec gzip -cd $file | tail $options
+
        exec gzip -cd ${files[1]} | tail $options
done
+
    else
 +
        for j in `seq 1 $count`; do
 +
            echo "==>"${files[j]}"<=="
 +
            exec gzip -cd ${files[j]} | tail $options
 +
        done
 +
    fi
 +
else
 +
    echo -ne "***Warning: no regular zip files given***\n\n"
 +
    exec echo "$usage"
 +
fi
 
</source>
 
</source>

Latest revision as of 11:21, 12 December 2011

Linux usually provides a couple of command piping gzip

zgrep, zcat, zdiff, zless, zmore, zegrep,...

Here is a simple extension of tail and head for zipped files, that you can call ztail and zhead (example is for tail, replace "tail" with "head" everywhere to get zhead) and add to your own /bin directory:

Caution:

very basic version, works when the options starts with '-' so it works with '-n10' but fails with '-n 10'. Should be easily improved...

#!/bin/bash                                                                                                                                                  

# This program is free software; you can redistribute it and/or modify                                                                                       
# it under the terms of the GNU General Public License as published by                                                                                       
# the Free Software Foundation; either version 2 of the License, or                                                                                          
# (at your option) any later version.                                                                                                                        

# This program is distributed in the hope that it will be useful,                                                                                            
# but WITHOUT ANY WARRANTY; without even the implied warranty of                                                                                             
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                                                                              
# GNU General Public License for more details.                                                                                                               

# You should have received a copy of the GNU General Public License along                                                                                    
# with this program; if not, write to the Free Software Foundation, Inc.,                                                                                    
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                                                                                                

# based on zless                                                                                                                                             

PATH=${GZIP_BINDIR-'/bin'}:$PATH

version="ztail 0.1                                                                                                                                           
Copyright (C) 2007, Free Software Foundation, Inc. <http://fsf.org/>                                                                                         
This is free software.  You may redistribute copies of it under the terms of                                                                                 
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.                                                                                       
There is NO WARRANTY, to the extent permitted by law.                                                                                                        
                                                                                                                                                             
Written by Guillaume Roux."

usage="Usage: $0 [OPTIONS]... [FILES]...                                                                                                                     
                                                                                                                                                             
Same as the 'tail' command, but operate on the uncompressed contents of any compressed FILEs.                                                                
Options are the same as for 'tail'."

case $1 in
--help)    exec echo "$usage";;
-h)        exec echo "$usage";;
--version) exec echo "$version";;
esac

options=""
count=0
for i in $@; do
str="$i"
if test -f $i && [ ${str:(-3)} = ".gz" ]; then
    files[++count]=$str
else
    if [ ${str:0:1} = "-" ] && [ $str != "-v" ] && [ $str != "--verbose" ]; then
	options=$options"$i "
    elif [ $str != "-v" ] && [ $str != "--verbose" ]; then
        echo -ne "***Warning: $i is neither a regular zip file or a regular option***\n\n"
    fi
fi
done

if [ $count -gt 0 ]; then
    if [ $count -eq 1 ]; then
        exec gzip -cd ${files[1]} | tail $options
    else
        for j in `seq 1 $count`; do
            echo "==>"${files[j]}"<=="
            exec gzip -cd ${files[j]} | tail $options
        done
    fi
else
    echo -ne "***Warning: no regular zip files given***\n\n"
    exec echo "$usage"
fi