Sponsored Content
Full Discussion: Ques
Top Forums Shell Programming and Scripting Ques Post 59003 by zazzybob on Tuesday 7th of December 2004 11:21:06 AM
Old 12-07-2004
Make sure you backslash escape the dot "." as it is a metacharacter (any char).

The code above will also match anything ending in an upper-case Z, e.g.

echo "something.ZAZZ" | grep '.Z$'
will match.

You need
echo "something.ZAZZ" | grep '\.Z$' (exit code 1)
echo "foo.log.Z" | grep '\.Z$' (exit code 0 - correct)

EDIT:

To get you on the right track. Let's assume you write a script, foo.sh. You call foo.sh something like this

$ ./foo.sh myfile

Where myfile can sometimes have a .Z extension.

Within your code, do something like (I've made this fairly verbose so you can see what's happening)

Code:
#!/bin/sh

inputfile=$1

echo "$inputfile" | grep '\.Z$' >/dev/null 2>&1
if [ "$?" -eq "0" ]; then
   # yay, it's been compressed
   zcat $inputfile | while read line
   do
     echo "Heres the line... $line"
   done
else
   # assume it's not compressed, you could check for .gz, etc
   while read line
   do
     echo "Heres the line... $line"
   done < $inputfile
fi

exit 0

This is untested, but should work.

Cheers
ZB

Last edited by zazzybob; 12-07-2004 at 12:31 PM..
 

5 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

ques regarding date command

i have couple of my assignment questions What command results in adding today's date and time to the file yesterday.txt What command results in creation of a new file named yesterday.txt that contains yesterday's date but no time (3 Replies)
Discussion started by: doomed47
3 Replies

2. Shell Programming and Scripting

Field separator Ques.

Hello... Im trying to use "- " as field separator... I used awk -F"- " '{print $3}' input_file ... but it's not working, it assumes that the field separator is "-" and not "- " ... Any ideas ?? :( Thanks (6 Replies)
Discussion started by: yahyaaa
6 Replies

3. UNIX for Dummies Questions & Answers

Fsck ques

hy guys I got a ques I cant acess root, i tried to fsck it, but gets errors to read file systems. What steps do you take to recover the host before you see if there is any data corruption on the root drive? Regards Charneet (1 Reply)
Discussion started by: charneet
1 Replies

4. UNIX for Dummies Questions & Answers

Service ques

hi, Can someone tell me what commands do you use to start service back upin solaris ? Regards Charneet (1 Reply)
Discussion started by: charneet
1 Replies

5. UNIX for Advanced & Expert Users

UNIX ques!! Ans URGENT!!

hello guys!! need 1 favour from u all.. Can u jst tell me the answers for these ques?? 1. ls - l _____ : command to return all files that end with single digit and those with TXT extension 2. ls -l report* _______ : command to return all files that start with the word RPT except those with LOG... (1 Reply)
Discussion started by: Gan_7
1 Replies
ZGREP(1)						      General Commands Manual							  ZGREP(1)

NAME
zgrep - search possibly compressed files for a regular expression SYNOPSIS
zgrep [ grep_options ] [ -e ] pattern filename... DESCRIPTION
Zgrep invokes grep on compressed or gzipped files. These grep options will cause zgrep to terminate with an error code: (-[drRzZ]|--di*|--exc*|--inc*|--rec*|--nu*). All other options specified are passed directly to grep. If no file is specified, then the standard input is decompressed if necessary and fed to grep. Otherwise the given files are uncompressed if necessary and fed to grep. If the GREP environment variable is set, zgrep uses it as the grep program to be invoked. EXIT CODE
2 - An option that is not supported was specified. AUTHOR
Charles Levert (charles@comm.polymtl.ca) SEE ALSO
grep(1), gzexe(1), gzip(1), zdiff(1), zforce(1), zmore(1), znew(1) ZGREP(1)
All times are GMT -4. The time now is 06:01 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy