how to grep word from .gz files & .z files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to grep word from .gz files & .z files
# 1  
Old 02-19-2008
how to grep word from .gz files & .z files

Hi I have find some account numbers from .gz files.

There thousands of .gz files, also i am unable to grep .gz / .z files.

I want to find file names & and those lines from list.

Could you pls tell me / give me any syntax for finding word from ,gz files using grep?

Thanks in advance
# 2  
Old 02-19-2008
zgrep test *
# 3  
Old 02-19-2008
It's not working

nuh8923:MERP:/apps/mercury/data/MERP_ARCHIVE/OUTBOUND/done> zgrep 20060324179536081 TBS20060325M25MB-6
ksh: zgrep: not found


am getting this error.
# 4  
Old 02-19-2008
try
man zgrep

reason being I could do that & mine is /bin/sh
# 5  
Old 02-19-2008
Heres the zgrep on my machine.

Just save this piece of code in your /usr/local/bin/zgrep and change the permission chmod +x /usr/local/bin/zgrep and use it

more /usr/local/bin/zgrep
#!/bin/sh

# zgrep -- a wrapper around a grep program that decompresses files as needed
# Adapted from a version sent by Charles Levert <charles@comm.polymtl.ca>

PATH="/usr/local/bin:$PATH"; export PATH

prog=`echo $0 | sed 's|.*/||'`
case "$prog" in
*egrep) grep=${EGREP-egrep} ;;
*fgrep) grep=${FGREP-fgrep} ;;
*) grep=${GREP-grep} ;;
esac
pat=""
while test $# -ne 0; do
case "$1" in
-e | -f) opt="$opt $1"; shift; pat="$1"
if test "$grep" = grep; then # grep is buggy with -e on SVR4
grep=egrep
fi;;
-*) opt="$opt $1";;
*) if test -z "$pat"; then
pat="$1"
else
break;
fi;;
esac
shift
done

if test -z "$pat"; then
echo "grep through gzip files"
echo "usage: $prog [grep_options] pattern [files]"
exit 1
fi

list=0
silent=0
op=`echo "$opt" | sed -e 's/ //g' -e 's/-//g'`
case "$op" in
*l*) list=1
esac
case "$op" in
*h*) silent=1
esac

if test $# -eq 0; then
gzip -cdfq | $grep $opt "$pat"
exit $?
fi

res=0
for i do
if test $list -eq 1; then
gzip -cdfq "$i" | $grep $opt "$pat" > /dev/null && echo $i
r=$?
elif test $# -eq 1 -o $silent -eq 1; then
gzip -cdfq "$i" | $grep $opt "$pat"
r=$?
else
gzip -cdfq "$i" | $grep $opt "$pat" | sed "s|^|${i}:|"
r=$?
fi
test "$r" -ne 0 && res="$r"
done
exit $res
netx@ndcnxg30% more zgrep
zgrep: No such file or directory
netx@ndcnxg30% more /usr/local/bin/zgrep
#!/bin/sh

# zgrep -- a wrapper around a grep program that decompresses files as needed
# Adapted from a version sent by Charles Levert <charles@comm.polymtl.ca>

PATH="/usr/local/bin:$PATH"; export PATH

prog=`echo $0 | sed 's|.*/||'`
case "$prog" in
*egrep) grep=${EGREP-egrep} ;;
*fgrep) grep=${FGREP-fgrep} ;;
*) grep=${GREP-grep} ;;
esac
pat=""
while test $# -ne 0; do
case "$1" in
-e | -f) opt="$opt $1"; shift; pat="$1"
if test "$grep" = grep; then # grep is buggy with -e on SVR4
grep=egrep
fi;;
-*) opt="$opt $1";;
*) if test -z "$pat"; then
pat="$1"
else
break;
fi;;
esac
shift
done

if test -z "$pat"; then
echo "grep through gzip files"
echo "usage: $prog [grep_options] pattern [files]"
exit 1
fi

list=0
silent=0
op=`echo "$opt" | sed -e 's/ //g' -e 's/-//g'`
case "$op" in
*l*) list=1
esac
case "$op" in
*h*) silent=1
esac

if test $# -eq 0; then
gzip -cdfq | $grep $opt "$pat"
exit $?
fi

res=0
for i do
if test $list -eq 1; then
gzip -cdfq "$i" | $grep $opt "$pat" > /dev/null && echo $i
r=$?
elif test $# -eq 1 -o $silent -eq 1; then
gzip -cdfq "$i" | $grep $opt "$pat"
r=$?
else
gzip -cdfq "$i" | $grep $opt "$pat" | sed "s|^|${i}:|"
r=$?
fi
test "$r" -ne 0 && res="$r"
done
exit $res
# 6  
Old 02-19-2008
If you lack zgrep you can do this:

zcat | grep STRING
# 7  
Old 02-20-2008
don't forget about gzgrep. I use it all the time.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy files based on specific word in a file name & its extension and putting it in required location

Hello All, Since i'm relatively new in shell script need your guidance. I'm copying files manually based on a specific word in a file name and its extension and then moving it into some destination folder. so if filename contains hyr word and it has .md and .db extension; it will move to TUM/HYR... (13 Replies)
Discussion started by: prajaktaraut
13 Replies

2. Shell Programming and Scripting

Find a word and increment the number in the word & save into new files

Hi All, I am looking for a perl/awk/sed command to auto-increment the numbers line in file, P1.tcl: run_build_model sparc_ifu_dec run_drc set_faults -model path_delay -atpg_effectiveness -fault_coverage add_delay_paths P1 set_atpg -abort_limit 1000 run_atpg -ndetects 1000 I would like... (6 Replies)
Discussion started by: jypark22
6 Replies

3. Shell Programming and Scripting

Delete all files if another files in the same directory has a matching occurrence of a specific word

he following are the files available in my directory RSK_123_20141113_031500.txt RSK_123_20141113_081500.txt RSK_126_20141113_041500.txt RSK_126_20141113_081800.txt RSK_128_20141113_091600.txt Here, "RSK" is file prefix and 123 is a code name and rest is just timestamp of the file when its... (7 Replies)
Discussion started by: kridhick
7 Replies

4. UNIX for Advanced & Expert Users

Find all files other than first two files dates & last file date for month

Hi All, I need to find all files other than first two files dates & last file date for month and month/year wise list. lets say there are following files in directory Mar 19 2012 c.txt Mar 19 2012 cc.txt Mar 21 2012 d.txt Mar 22 2012 f.txt Mar 24 2012 h.txt Mar 25 2012 w.txt Feb 12... (16 Replies)
Discussion started by: Makarand Dodmis
16 Replies

5. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

6. Shell Programming and Scripting

awk & grep - check for a value and write sub-word

Hi people, I have a file status.txt: Following 6 ports are totally or partially unavailable: ------------------------------------------------------------ MOD LINK PORTNAMES STAT1 STAT2 STAT3 SYN TYPE ------------------------------------------------------------ 8 Pr37 ... (12 Replies)
Discussion started by: gc_sw
12 Replies

7. Shell Programming and Scripting

How to compare 2 files & get only few columns based on a condition related to both files?

Hiiiii friends I have 2 files which contains huge data & few lines of it are as shown below File1: b.dat(which has 21 columns) SSR 1976 8 12 13 10 44.00 39.0700 70.7800 7.0 0 0.00 0 2.78 0.00 0.00 0 0.00 2.78 0 NULL ISC 1976 8 12 22 32 37.39 36.2942 70.7338... (6 Replies)
Discussion started by: reva
6 Replies

8. Shell Programming and Scripting

Delete all files if another files in the same directory has a matching occurence of a specific word

Hello, I have several files in a specific directory. A specific string in one file can occur in another files. If this string is in other files. Then all the files in which this string occured should be deleted and only 1 file should remain with the string. Example. file1 ShortName "Blue... (2 Replies)
Discussion started by: premier_de
2 Replies

9. Shell Programming and Scripting

grep one word in two files

Hi All, Can we grep one word in two file at same time. i.e using one command only. for ex egrep -i "word" file1 file2 is it possible? (1 Reply)
Discussion started by: sam25
1 Replies

10. Shell Programming and Scripting

how to grep a word from files in different folders

Hi, I have 10 folders like a1,a2,a3...a10 each folder having 100 files. how to grep a word from all the files in all folders at a time? (8 Replies)
Discussion started by: suresh3566
8 Replies
Login or Register to Ask a Question