I am very new to bash. So if this is a pretty basic question, please pardon me.
I am trying to replace file extension ‘.gzip‘ with ‘.gz‘.
E.g.:
testfile.xml.gzip => testfile.xml.gz
Someone has written a script which does this:
GZIP=`echo ${FILE} | grep .gz`
.
.
.
FILE=`echo ${FILE} | sed 's/.gz//g'`
The first line wrongly matches testfile.xml.gzip file. The grep .gz matches the text in filename which is in-between, whereas I need it to match only if it is at the end of the filename. Can anyone help me with how to correct this problem? In short, I need to know the expression which matches pattern the end of the line/text.
Use
$to match the end of the line:Anyway, what this command does is remove the trailing
.gzextension from the filename, which isn’t what you’re looking for according to your question. To do that, the answer from dnsmkl is the way to go withsed.Note that since you already have
FILEin a enviroment variable you can use bash string manipulation as follows: