Delete a "G" character from df -h result


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Delete a "G" character from df -h result
# 1  
Old 03-29-2019
Delete a "G" character from df -h result

Hello everybody,


I'm a new user in forum,



In a script, i would like that the result sort :
Code:
/usr - 665 Go (free: 631 Go / 6% used)

but i obtain :
Code:
/usr - 665G Go (free: 631G Go / 6% used)

My command line is :
Code:
RES_DF=`df -BG /usr/ | tail -n1 | awk -vOFS='' '{print $5, " - ", $1, " Go (free: ", $3, " Go / ",$4, " used)"}'`

Cause :
DF sort size in GO with the "G" character.

Question :
Do you have an idea for me to get what I want...?


Thank's a lot




Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 03-29-2019 at 08:49 AM.. Reason: Added CODE tags.
# 2  
Old 03-29-2019
Welcome to the forum.


Please stick to the rules (e.g. use code tags), and be careful and consistent when presentig your problem (e.g. df -h in the title while df -BG in the post).


For your problem, none of the two, be it df -h or df -BG, is the option of choice, as the results displayed may suffer from improper rounding effects, and you always ask yourself are those units for 2^30 or 1E9 multipliers. Use blocks or bytes instead, and calculate to taste.


To remove the G character, use awk's sub() function for every field that it applies to, e.g. sub (/G/, "", $3).
If using df -B1, try int($3/2^30+.5)

Last edited by RudiC; 03-29-2019 at 09:12 AM..
# 3  
Old 03-29-2019
Hi,



Ok, thanks for your help and sorry for incoherence of my title...


I almost got what i want with :


Code:
RES_DF=`df "/usr/" | grep "/usr/" | awk -vOFS='' '{print $5, " - ", $1/1024/1024, " Go (free: ", $3/1024/1024, " Go / ",$4, " used)"}'`


Reslult :  /usr/ - 4.80615 Go (free: 3.05335 Go / 34% used)

Except now I want to round to one number after the comma Smilie
# 4  
Old 03-29-2019
Try setting the output format string: -vOFMT="%.1f".
This User Gave Thanks to RudiC For This Post:
# 5  
Old 03-29-2019
Very good Rudic, it works!



Thank a lot and have a nice weekend!


Smilie
This User Gave Thanks to xnyp For This Post:
# 6  
Old 03-29-2019
I think you struggle with the GNU df output over two lines even if it's not a terminal.
A good fix is df -kP
Code:
df -kP /usr | awk '/^\// { printf "%s - %.1f Go (free: %.1f Go / %s used)\n", $6, $2/1024/1024, $4/1024/1024, $5 }'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies

2. Shell Programming and Scripting

Find "*.c" and "Makefile" and then delete them with one line

find "*.c" and "Makefile" and then delete them with one line (3 Replies)
Discussion started by: yanglei_fage
3 Replies

3. Shell Programming and Scripting

Delete characters from each line until meet character ":"

Hello, I have file that looks like this : 765327564:line1 94:line2 7865:line3 ..... 765322:linen I want to cut all the digits from the beginning of each line up to ":" character and to have everything like this : line1 line2 line3 ..... linen P.S : content of line1 ...... (8 Replies)
Discussion started by: black_fender
8 Replies

4. Shell Programming and Scripting

Using sed to find text between a "string " and character ","

Hello everyone Sorry I have to add another sed question. I am searching a log file and need only the first 2 occurances of text which comes after (note the space) "string " and before a ",". I have tried sed -n 's/.*string \(*\),.*/\1/p' filewith some, but limited success. This gives out all... (10 Replies)
Discussion started by: haggismn
10 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

Delete files older than "x" if directory size is greater than "y"

I wrote a script to delete files which are older than "x" days, if the size of the directory is greater than "y" #!/bin/bash du -hs $1 while read SIZE ENTRY do if ; then find $1 -mtime +$2 -exec rm -f {} \; echo "Files older than $2 days deleted" else echo "free Space available"... (4 Replies)
Discussion started by: JamesCarter
4 Replies

7. Shell Programming and Scripting

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

8. UNIX for Advanced & Expert Users

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

9. UNIX for Dummies Questions & Answers

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

10. Shell Programming and Scripting

Unix commands delete all files starting with "X" except "X" itself. HELP!!!!?

im a new student in programming and im stuck on this question so please please HELP ME. thanks. the question is this: enter a command to delete all files that have filenames starting with labtest, except labtest itself (delete all files startign with 'labtest' followed by one or more... (2 Replies)
Discussion started by: soccerball
2 Replies
Login or Register to Ask a Question