Visit The New, Modern Unix Linux Community

Remove trailing G

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove trailing G
# 1  
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.

# 2  
You can remove the trailing character as follow:

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

# 3  
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!

# 4  
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 }'


Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #535
Difficulty: Medium
In C, the Boolean type false is associated with zero while any non-zero value is associated with true.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove trailing number

I have some strings such as ABC1 ABC2 TYFASDD12 They will only have letters and numbers. In each case I want to remove the last digit? The lengths will vary. So a hard coded substr won't work. What do I do? if it doesn't end in a number, I don't want to remove any characters. (6 Replies)
Discussion started by: guessingo
6 Replies

2. Shell Programming and Scripting

Remove trailing space

Hi I am trying to remove trailing space from a string. value=${value%% } It is not working. What might be the issue with the above snippet. (7 Replies)
Discussion started by: munna_dude
7 Replies

3. Shell Programming and Scripting

Remove trailing 0 from the field

Hi Freinds, I have file1.txt as below file1.txt 1521894~~-0.400~201207 1521794~~-0.486~201207 152494~~-0.490~201207 152154894~~-0.490~201207 1521894354~~-0.489~201207 expected output : 1521894~~-0.4~201207 1521794~~-0.486~201207 152494~~-0.49~201207... (9 Replies)
Discussion started by: i150371485
9 Replies

4. Shell Programming and Scripting

Remove trailing zeros

Hi I have a simple request but can't find the answer. I want to remove trailing zeros, and in some cases the fullstops, from the input data. Example of input file: FR002_15.000_20.000 SD475_5.000_10.500 FG5647_12.250_15.500 BH2463_30.555_32.000 Desired output file would be: ... (10 Replies)
Discussion started by: theflamingmoe
10 Replies

5. Shell Programming and Scripting

Remove of extra spaces from the trailing

HI, I need the help from the experts like I have created one file with text like: a b c d e f g h i j k l So my question is that i have to write the script in which like in the first sentence it will take only one space after d and remove all the extra space in the end.I dont... (8 Replies)
Discussion started by: bhanudhingra
8 Replies

6. Shell Programming and Scripting

How to remove trailing spaces from a variable?

I am getting a value from a csv file using CUT command, however the command extracting the records with trailing spaces. I am using the result into a sql session to fetch data, because of the trailing spaces the sql session is unable to fetch any data. Please let me know, how to remove this... (2 Replies)
Discussion started by: mady135
2 Replies

7. Shell Programming and Scripting

Remove trailing spaces from file

I'm currently writing my sql results to a file and they have trailing spaces after each field. I want to get rid of these spaces and I'm using this code: TVXTEMP=$(echo $TVXTEMP|sed -e 's/\ //g') It doesn't work though. I'm not familiar with sedscript, and the other codes I've found online... (6 Replies)
Discussion started by: avillanueva
6 Replies

8. Shell Programming and Scripting

remove trailing spaces from a line

I want to remove the trailing spaces from any line of file. line ending does not follow any pattern. plz help (3 Replies)
Discussion started by: vikas_kesarwani
3 Replies

9. UNIX for Dummies Questions & Answers

How to remove trailing spaces

Hi, I have a file like this (ADD_MONTHS((Substr(Trim(BOTH FROM Translate(Maximum(closeDa ------------------------------------------------------------ 2007-06-30 00:00:00 I have a requirement where i need just the date. When i do: tail -1... (2 Replies)
Discussion started by: mahek_bedi
2 Replies

10. Shell Programming and Scripting

remove trailing newline characters

Hello , I have the folowing scenario : I have a text file as follows : (say name.txt) ABC DEF XYZ And I have one more xml file as follows : (say somexml.xml) <Name>ABC</Name> <Age>12</Age> <Class>D</Class> <Name>XYZ</Name> <Age>12</Age> <Class>D</Class> <Name>DEF</Name>... (7 Replies)
Discussion started by: shweta_d
7 Replies

Featured Tech Videos