illcar
November 20, 2008, 6:29pm
1
I have myMethod that gives me available,used,free disk space in KB. I parse the used disk space using awk. That gives me something like 830,016. I want the output to be 830016 so that I can add 100000 to it. In other words I would like to use used_space variable in numeric calculations (using expr).
.....
myMethod
used_space="`myMethod | grep sum | awk -F' ' '{print $3}'`"
echo $used_space
echo ${used_space/,/}
But the last line above gives me error:
sum 1,977,920 830,016 1,147,904
830,016
./test.sh: bad substitution
What can I do? If I do the same thing on command line it works fine!
bash-3.00# export abc=830,123
bash-3.00# echo ${abc/,/}
830123
bash-3.00#
danmero
November 20, 2008, 6:58pm
2
Let awk do the job and don't get complicated.
used_space=`myMethod | awk '/sum/{gsub(/,/,"");print $3}'`
I guess you havn't specify the shell in script. (Do not remember how that line is named..)
First line in script
#! /usr/bin/bash
for example.
In bash it works fine, but I have tried it in ksh:
> ec $used_space
830,016
> ec ${used_space/,/}
ksh: ${used_space/,/}: bad substitution
>
illcar
November 21, 2008, 6:19pm
4
I am sorry, I am still having issues. I changed the code as follows:
myMethod
used=`myMethod | awk '{gsub(/,/,""); print $3}'`
echo $used
Now I get a different error. If it is not clear what could be wrong, any pointers for debugging awk would be appreciated.
bash-3.00# ./test.sh
sum 1,977,920 795,648 1,182,272
awk: syntax error near line 1
awk: illegal statement near line 1
bash-3.00# stty: : I/O error
The first line of the script is:
#!/bin/sh
problem is:
1: 'awk' does not know the function 'gsub()'
use 'nawk'
2: why you do not change the 'sh' to 'bash' - everything will be simpler
illcar
November 24, 2008, 2:14pm
6
Thanks, nawk did the trick.