Click to See Complete Forum and Search --> : deleting letters with awk


ecks
05-21-2004, 05:56 PM
I am currently writing a script that untars a certain file specified. It makes a directory with the same name as the file, without the tar.gz part. For example, the file is foo.tar.gz. It makes a directory called foo. Now, I want to store that name, and in order to achieve that, I have to erase the .tar.gz part somehow. How would I be able to achieve that? Much help is appreciated.

bwkaz
05-21-2004, 06:35 PM
If this is a bash script, bash has some text manipulation ability:

filename=blah.tar.gz

dirname=${filename%%.tar.gz}

echo $dirname This will print out "blah". The %% tells bash to expand ".tar.gz" like it was a filename (so it does globbing, so you can use *'s to represent multiple characters if you ever need to), then remove the longest expansion of ".tar.gz" (which right now, is ".tar.gz") from the end of the contents of the variable $filename.

If you use a single % sign instead of two, it matches up the shortest expansion of ".tar.gz" instead of the longest. If you use # signs instead of % signs, then the removal happens at the beginning of $filename instead of the end.

ecks
05-21-2004, 07:17 PM
ok, thanx a lot. That worked!