Remove trailing G

Hello, I am trying to write a script that will calculate the amount of data remaining in a storage volume. I'm running Tru64 Unix version 5.1B patch kit 6. The script is being run against an AdvFS domain. I am programming in Korn Shell version M-11/16/88f.

The basic idea is that I want to run df -h and grep for the domain in question. I then pipe that output into awk to extract the two fields I want and store them in variables. I've made it this far with my script.

So now I have two variables: total and remaining. Both hold a number followed by the letter G (for Gigabytes).

I want to remove the trailing G and then take the 2 numbers that are left and print the difference. What I'm stumped on is how to remove the trailing 'G'.

I know that in sed, I could do something like:

new_variable=`sed 's/[0-9].*G$//g'`

to remove the trailing G, but to my knowledge you can't pass a shell variable into sed, so I don't think the following would work:

total=`sed 's/${total}$//g'`

So now my program has 2 variables that both hold a number followed immediately by the letter G. I know I could probably use cut, but the number of characters differs each week. This week, total might be 4 characters with a trailing G, next week it might only be 3. What tool can I use to remove the 'G' and save just the number back into the variable? I think if I could make it passed this step I could figure the arithmetic part out.

Thanks.

You can remove the trailing character as follow:

sed 's/\(.*\)./\1/'

Regards

That did the trick! I added the sed command to the end of the pipeline I was using to gather the data. So my pipeline looks like:

df -h | grep <search string> | tail -1 | awk '{ print $2 }' | sed 's/\(.*\)./\1/'

That delivers exactly what I had in mind. Thank you!

:slight_smile:

For the sed command you can also do :

sed 's/.$//'

You can combine grep|tail|awk|sed pipeline into one awk command :

df -h| awk '/search string/ {res=$2} END { sub(/.$/,"", res); print res }'

Jean-Pierre.