The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 07:48 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-20-2008
illcar illcar is offline
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#
  #2 (permalink)  
Old 11-20-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,419
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}'`
  #3 (permalink)  
Old 11-20-2008
alex_5161 alex_5161 is offline
Registered User
  
 

Join Date: Jan 2007
Location: Detroit
Posts: 124
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
>
  #4 (permalink)  
Old 11-21-2008
illcar illcar is offline
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
  #5 (permalink)  
Old 11-21-2008
alex_5161 alex_5161 is offline
Registered User
  
 

Join Date: Jan 2007
Location: Detroit
Posts: 124
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
  #6 (permalink)  
Old 11-24-2008
illcar illcar is offline
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.
Sponsored Links
Closed Thread

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:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:03 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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