The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
String manipulation using ksh zambo UNIX for Dummies Questions & Answers 5 09-18-2008 01:25 PM
String manipulation in TCL jehrome_rando Shell Programming and Scripting 1 07-15-2008 08:23 AM
How to use sed for string manipulation hikrishn Shell Programming and Scripting 2 06-16-2008 11:43 AM
String manipulation jerrynimrod Shell Programming and Scripting 2 06-05-2008 11:06 AM
String manipulation mr_bold UNIX for Dummies Questions & Answers 2 02-14-2007 06:48 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-20-2008
Registered User
 

Join Date: Nov 2008
Posts: 9
string manipulation issue

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).

Code:
.....
myMethod
used_space="`myMethod | grep sum | awk -F' ' '{print $3}'`"
echo $used_space
echo ${used_space/,/}
But the last line above gives me error:
Code:
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!
Code:
bash-3.00# export abc=830,123
bash-3.00# echo ${abc/,/}
830123
bash-3.00#
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-20-2008
danmero danmero is offline Forum Advisor  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 901
Quote:
Originally Posted by illcar View Post
Code:
used_space="`myMethod | grep sum | awk -F' ' '{print $3}'`"
Let awk do the job and don't get complicated.
Code:
used_space=`myMethod | awk '/sum/{gsub(/,/,"");print $3}'`
Reply With Quote
  #3 (permalink)  
Old 11-20-2008
Registered User
 

Join Date: Jan 2007
Location: Detroit
Posts: 109
I guess you havn't specify the shell in script. (Do not remember how that line is named..)
First line in script
Code:
#! /usr/bin/bash
for example.

In bash it works fine, but I have tried it in ksh:
Code:
> ec $used_space
830,016
> ec ${used_space/,/}
ksh: ${used_space/,/}: bad substitution
>
Reply With Quote
  #4 (permalink)  
Old 11-21-2008
Registered User
 

Join Date: Nov 2008
Posts: 9
I am sorry, I am still having issues. I changed the code as follows:

Code:
 
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.

Code:
 
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:
Code:
 
#!/bin/sh
Reply With Quote
  #5 (permalink)  
Old 11-21-2008
Registered User
 

Join Date: Jan 2007
Location: Detroit
Posts: 109
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
Reply With Quote
  #6 (permalink)  
Old 11-24-2008
Registered User
 

Join Date: Nov 2008
Posts: 9
Quote:
Originally Posted by alex_5161 View Post
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
Thanks, nawk did the trick.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Bookmarks

Tags
shell script, shell scripting, unix scripting, unix scripting basics

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:




All times are GMT -4. The time now is 04:05 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66