How to substitute in a large gz file?

I have a large .gz file where i wish to replace the occurrence of a particular word with another word.. Can anyone help?I prefer a short script..

Can sed work with gz files?

As far as I know, You have to gunzip the file. since you want to edit the file contents.

gunzip the file.
do your things.
gzip it back.

That is correct. You can combine the operations in a pipeline though:

gunzip < file.gz | sed -e 's/search_str/replace_str/g' | gzip -c > temp.gz
mv temp.gz file.gz
1 Like

this will also work

gunzip ur_file.gz | awk 's/ur_string/replace_string/g' | gzip -c final.gz
mv final.gz ur_xfile.gz

why gunzip , try using

gzcat filename.gz | sed 's/MATCH/REPLACE/g' |gzip -c output.gz

Because a) it's the same thing, b) people will have it. My system calls it zcat not gzcat, and on closer inspection, it's just a shell script that does exec gunzip -c "$@" . Many systems don't have either. So you're not saving anything by using gzcat and causing more problems.