string manipulation issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting string manipulation issue
# 1  
Old 11-20-2008
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  
Old 11-20-2008
Quote:
Originally Posted by illcar
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  
Old 11-20-2008
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  
Old 11-21-2008
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  
Old 11-21-2008
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  
Old 11-24-2008
Quote:
Originally Posted by alex_5161
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Deleting part of a string : string manipulation

i have something like this... echo "teCertificateId" | awk -F'Id' '{ print $1 }' | awk -F'te' '{ print $2 }' Certifica the awk should remove 'te' only if it is present at the start of the string.. anywhere else it should ignore it. expected output is Certificate (7 Replies)
Discussion started by: vivek d r
7 Replies

2. Shell Programming and Scripting

string manipulation

hi all, i am new to shell scripting and need help. i have a string that stores the month in Jan/Feb/Mar format. i need to convert it to number like 01 for jan, 12 for dec etc. i am using the following sed command, echo "Enter a Month eg. Jan/Feb : " read MONTHEND ... (9 Replies)
Discussion started by: anupom2000
9 Replies

3. UNIX for Dummies Questions & Answers

Help with String manipulation

Dear All, I have a question. I have files with the following pattern.>S8_SK1.chr01 NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN NNNNNNNNNNNNNNNNNNNCAGCATGCAATAAGGTGACATAGATATACCCACACACCACACCCTAACACTAACCCTAATCTAACCCTGGCCAACCTGTTT... (13 Replies)
Discussion started by: pawannoel
13 Replies

4. Shell Programming and Scripting

String Manipulation

Write a shell program to display the position of the right - most character in a given input string. Example : Input : RAHUL Output : L is in the 5th position also tell me how to count length of string and how to find the position of specific character in left most side. (1 Reply)
Discussion started by: shashwat2691
1 Replies

5. UNIX for Dummies Questions & Answers

String manipulation

I am doing some training for a job I have just got and there is an exercise I am stuck with. I am not posting to ask a question about logic, just a trivial help with string manipulation. I would appreciate if somebody could at least give me a hint on how to do it. Basically, the intelligent part... (8 Replies)
Discussion started by: Dantastik
8 Replies

6. Shell Programming and Scripting

String manipulation

Hi, i am just gettin exposed to UNIX. Could anyone of u help me out with dis problem..? i have a variable 'act' which has the value as follows, echo $act gives -0- -0- -----0---- 2008-06-04 -0- -0- echo "$act" | awk '{print ($act)}' gives, -0- -0- -----0---- 2008-06-04 -0- -0- I... (2 Replies)
Discussion started by: jerrynimrod
2 Replies

7. UNIX for Dummies Questions & Answers

string manipulation

Hi, I have a file with rows of text like so : E100005568374098100000015667 D100005568374032000000112682 H100005228374060800000002430 I need to grab just the last digits(bolded) of each line without the proceeding text/numbers. Thanks (5 Replies)
Discussion started by: james6
5 Replies

8. Shell Programming and Scripting

String Manipulation Help

Hey Guys, Right i know how to alter a word to begin with a capital letter, i know how to remove unwanted characters and replace them with the relevant character however i don't now if there is a way to do them all in one line. Code: echo -n ${string:0:1} | tr a-z A-Z #convert first letter... (4 Replies)
Discussion started by: shadow0001
4 Replies

9. Shell Programming and Scripting

String manipulation

Hi There! I have the following block of text in my input file and in order to parse it correctly, i need to have i.e. If a line starts with a number, ignore it else replace it with the number from the previous line until the first ',' So, for example, 15,9:5/12345 ,10:1 ... (5 Replies)
Discussion started by: orno
5 Replies

10. Shell Programming and Scripting

string manipulation

Hello, I have a korn shell string variable str1 = "A,B,Z" I would like to create another korn shell string variable str2 = "letter = 'A' or letter = 'B' or letter = 'Z' " Please help! Thanks in advance an UNIX newbie! (13 Replies)
Discussion started by: hai1973
13 Replies
Login or Register to Ask a Question