Ques


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ques
# 1  
Old 12-07-2004
Question Uncompress

If a file is compressed, how can i uncompress on the fly using pipes(handling this in the shell script depending on the command line arguments passed to the script)

ex: ex_pr.sh -d disable -f ex_pr_1212.log.Z

Last edited by dreams5617; 12-07-2004 at 09:15 AM..
# 2  
Old 12-07-2004
This is all very dependent on how your shell script handles this. Seeing as you are supplying the compressed file as an argument to option "-f", then I would assume that the actual file opening and processing is handled internally within the script.

If, however, the script read from stdin, you could do
zcat somefile.Z | myscript

You can't do
myscript -f `zcat somefile.Z`
because that would expand the whole file and supply each word as an argument. Definitely not what you want!

You could add a handler to your shell script that looks at the supplied file's extension, and invokes zcat to read the file if it has a ".Z" extension.

EDIT: Please give your threads a more descriptive title than just "Ques". A descriptive, useful title will help people who have the appropriate knowledge find your post and help you out. Something like "Compressed file as a script argument" or similar would have been better for this post.

Cheers
ZB

Last edited by zazzybob; 12-07-2004 at 08:49 AM..
# 3  
Old 12-07-2004
If i pass ex_p1.log.Z as the argument at the command line
i want inside the script ex_p1.log(as the file name), i.e checking if the file extension is .Z(how ?), if it has .Z i want to uncompress so
ex_p1.log.Z is replaced as ex_p1.log (using pipes on fly -how ?)

Thanks
# 4  
Old 12-07-2004
$ echo "ex_pr_1212.log.Z" | grep ".Z$"
ex_pr_1212.log.Z
$ echo $?
0
$ echo "ex_pr_1212.log" | grep ".Z$"
$ echo $?
1
$
# 5  
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..
# 6  
Old 12-07-2004
$uncompress ex_pr1.dmp.Z > ex_pr12.dmp

I want to write the o/p of .Z to a different file name like ex_pr12.dmp

I tried like this but the ex_pr12.dmp has 0 bytes.
If there is a way to write to another file name please let me know.

Thanks
# 7  
Old 12-07-2004
u can try either

uncompress -c x.Z > somefile

or

zcat x.Z > somefile


in both the cases, u still have x.Z unmmodified.

U can remove it if u want ...
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. 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

2. 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

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. 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

5. 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
Login or Register to Ask a Question