Solved

Linux: Get rid of % when using grep and awk

Posted on 2013-11-15
3
384 Views
Last Modified: 2013-11-15
I use this to trim the % from the end of a string:
x="10%"
echo ${x%\%}

Open in new window


I use this to return results from df in JSON format:
df |grep -iv "filesystem" | awk '{print"\""$1"\":"$5"," }'

Open in new window


How can these two concepts be combined?  I want to remove % from the JSON results.
0
Comment
Question by:hankknight
  • 2
3 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 39651013
No need for "grep".

df | awk '! /[Ff]ilesystem/ {sub("%","",$5); print"\""$1"\":"$5"," }'

"sub" means "substitute". All we have to do is replacing the "%" sign with the empty string "".
wmp
0
 
LVL 20

Expert Comment

by:carlmd
ID: 39651015
I don't see a % sign in the result of

df |grep -iv "filesystem" | awk '{print"\""$1"\":"$5"," }'

Can you post an example of what you see?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39651029
I think hankknight's "df" behaves like "df -P" (POSIX format, where the 5th column is "%Used").
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now