Remove % from a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove % from a variable
# 1  
Old 09-08-2011
Data Remove % from a variable

I have the following sh file which I originally wrote for HP Unix(which worked great)... but df is different in AIX (HP doesn't have a % in df -k), So I need to remove the % for $x at the moment I'm getting 68% I need it to be 68 so my script complete.

Any help would be welcome.. I'm sure this is a simple thing that slipped my mind..Smilie

Code:
#!/bin/ksh
# This script can be used to warn the users that the file system is getting full
#
# Below is set to monitor all the file systems mounted and report to RECEIVER
#
# Usage: as a cron entry for best use.
#set -x
RECEIVER=bja@blank.co.za,gk@blank.co.za,bki@blank.co.za
y=90
# Exclude list of unwanted monitoring, if several partions then use "|" to separate the list
EXCLUDE_LIST="/opt/IBM/TPM|/opt/IBM/SCM|/opt/IBM/ITM"
#LIST=`cat /etc/fstab|grep -v ^#|awk '{print $2}'|grep -vxE "${EXCLUDE_LIST}"`
#LIST=`cat /etc/fstab|grep -v ^#|awk '{print $2}'`
for fs in `df |awk '{ print $NF }'|grep -vxE "${EXCLUDE_LIST}"`
do
     #echo fs=$fs
     x=`df -k $fs |sed -n " p"| awk '{ print $4 }'`
     echo $x
     #echo $y
     if [ $x -gt $y ]
     then
          message="File System $fs on `hostname` is $x% used."
          #echo $subject
          echo $message | mailx -s "`hostname` - File System Full Warning !!!" $RECEIVER
     fi
done


Moderator's Comments:
Mod Comment Please use [code] and [/code] tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks. Also refrain from overly use of font formattings as it makes editing a post very awful. Check your PMs.

Last edited by zaxxon; 09-08-2011 at 05:09 AM..
# 2  
Old 09-08-2011
Code:
$ echo "85%" | sed 's/%//'
85

---------- Post updated at 12:51 PM ---------- Previous update was at 12:46 PM ----------

you can try this also

Code:
 
x=`df -k $fs | awk '{ if(NR==2){print substr($4,length($4)-2,length($4)-1)}}'`

# 3  
Old 09-08-2011
Nope don't

Thanks for responce...
Code:
df -k $fs | awk '{ if(NR==2){print substr($4,length($4)-2,length($4)-1)}}'`

nope both your suggestions don't work come back to a >

Last edited by zaxxon; 09-08-2011 at 05:10 AM.. Reason: code tags, check PM
# 4  
Old 09-08-2011
can you post the output of df -k $fs ( any one mount point )
# 5  
Old 09-08-2011
Found solution

Code:
x=`df -k $fs |sed -n "2 p"| awk '{ print $4 }'| sed 's/%//'`

not probally best practice but it works...thanks

Last edited by zaxxon; 09-08-2011 at 05:11 AM.. Reason: code tags, check your PM
# 6  
Old 09-08-2011
Quote:
Originally Posted by KevinGod
x=`df -k $fs |sed -n "2 p"| awk '{ print $4 }'| sed 's/%//'`

not probally best practice but it works...thanks
It's not a terrible solution, but it isn't the best it could be. Filtering AWK's input by line number using sed is unnecessary; AWK can do that easily. Ditto for deleting a character from a string.
Code:
df -k "$fs" | awk 'NR==2 { sub(/%/, "", $4); print $4 }'

Regards,
Alister

Last edited by alister; 09-08-2011 at 05:03 AM..
# 7  
Old 09-08-2011
Quote:
Originally Posted by alister
Code:
df -k "$fs" | awk 'NR==2 { sub(/%/, "", $4); print $4 }'

Code:
df -k "$fs" | awk 'NR==2 {print int($4) }'

should do the job Smilie
This User Gave Thanks to danmero For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove the \r \n from the variable

I a have a delimited file that has a 2 column which I need to read and pull out both columns from the file.Now the while pulling out the second column and storing in a variable I get a \r\n charachter embedded in the output when an od -c is run.How can I remove the \r\n columns from the file. ... (3 Replies)
Discussion started by: Nikhil Gautam
3 Replies

2. UNIX for Dummies Questions & Answers

Remove newline char from variable

I have a file ABC.DAT with 2 columns avaialble Data format : XYZ!$#$!120 XXZ!$#$!1000 YYZ!$#$!104 While running the following code : FILE_COUNTER=1; RECORD_CN_FILE_COUNT=$((`wc -l ABC.DAT| cut -f1 -d' '`)); while do FILE_NAME=`cat ABC.DAT.DAT| head -$FILE_COUNTER |tail -1 | awk -F... (1 Reply)
Discussion started by: Nikhil Gautam
1 Replies

3. Shell Programming and Scripting

Remove appending 0 from file variable

Dear All, i have filename RYK3201_032001002.pdf and i am using below command to get a file file_name1=$(echo $file_name | cut -d "_" -f2 | cut -d "." -f1 | cut -c -6) and then file_name2=${NewFile_NAME}_$file_name1 now the value of file_name1 will be 032001 i want to file_name1... (5 Replies)
Discussion started by: yadavricky
5 Replies

4. Shell Programming and Scripting

How to remove new line from variable?

I have following codes: ~$ var_1="ABC" ~$ echo $var_1 | wc -m 4 ~$ echo -n $var_1 | wc -m 3 ~$ var_2=`echo -n $var_1` ~$ echo $var_2 ABC ~$ echo $var_2 | wc -m 4 ~$ I would suppose the last command ~$ echo $var_2 | wc -m would give 3; but apparently, var_2 still has the new line... (5 Replies)
Discussion started by: littlewenwen
5 Replies

5. Shell Programming and Scripting

PERL : Remove spaces in a variable

I have a variable I want to remove the spaces in between. The output should be How can this be done Any help will be appreciated. Thanks in advance (1 Reply)
Discussion started by: irudayaraj
1 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

How to remove the first character on a string in a variable

Hi all, Does anyone know how to code in ksh that will remove the first character in a string variable and replace that variable without the first character? Example: var1=ktest1 will become var1=test1 var2=rtest2 will become var2=test2 Need help please. (10 Replies)
Discussion started by: ryukishin_17
10 Replies

8. UNIX for Dummies Questions & Answers

Remove characters from string variable

I am running a script where one of the variables (the month and year) is input at the command line. What I would like to do is chop off the last few characters of that string to create a new variable, while maintaining the old one. The script is run like this: ./pull_station_v4.csh KYWST... (3 Replies)
Discussion started by: TheSMan5
3 Replies

9. Shell Programming and Scripting

Remove non numeric values from a variable

Hello all, I am working on a basic script but need a little help. Issue: I am running a SQL Query using sqlplus and a shell script. I have the output of the statement stored as variable $A. $A is set to "other text here 45678754 other text here". I need to strip all text except that numeric... (13 Replies)
Discussion started by: ownedthawte
13 Replies

10. Shell Programming and Scripting

Variable has spaces around the string, need to remove them

Hi all, I'm a newbie to the Linux world and I got a couple of shell script questions: (1) How do combine two variables and make it equal to a third variable? For example, I got a variable $A=FirstName, $B=LastName, and I want to combine the variable into one variable so when you echo the final... (4 Replies)
Discussion started by: mikey20
4 Replies
Login or Register to Ask a Question