Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Fri 16 Nov 2018, 18:28
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
How to get specific results from 'free -m' ?
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 3 [45 Posts]   Goto page: 1, 2, 3 Next
Author Message
ITSMERSH


Joined: 02 May 2018
Posts: 458

PostPosted: Wed 15 Aug 2018, 23:58    Post subject:  How to get specific results from 'free -m' ?  

Hi.

I need to get some specific results from 'free -m' into a variable.

Output 'free -m' wrote:
total used free shared buffers
Mem: 3987 1490 2497 0 3
-/+ buffers: 1486 2501
Swap: 9039 99 8940

I need those values marked bold - which is total memory / free memory.

Is there a simple and/or easy way to achieve?

Thanks

Edit:

After a some time of try and fail I could achieve it that way:

Quote:
FREERESULT="`free -m`"
TOTAL=$(echo $FREERESULT|cut -d':' -f2|cut -d' ' -f2)
FREE=$(echo $FREERESULT|cut -d':' -f2|cut -d' ' -f4)


Since there's two more lines including ':', is this secure?

Is there something smarter, a one liner probably?

_________________
RSH
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1678
Location: Japan

PostPosted: Thu 16 Aug 2018, 01:10    Post subject: Re: How to get specific results from 'free -m' ?  

ITSMERSH wrote:
Is there something smarter, a one liner probably?
Isn't your solution smart enough? Sure you could assign 2 variables in one line, but the result would look ugly.

Another way:
Code:
FREERESULT=(`free -m`)
TOTAL=${FREERESULT[6]}
FREE=${FREERESULT[8]}
Back to top
View user's profile Send private message 
01micko


Joined: 11 Oct 2008
Posts: 8680
Location: qld

PostPosted: Thu 16 Aug 2018, 01:17    Post subject:  

You can make a one-liner with read (and grep). Note that using read like this is a bashism.

Code:
# read a b c d e f g <<<$(free -m|grep '^Mem')
# echo $b $d

_________________
Puppy Linux Blog - contact me for access
Back to top
View user's profile Send private message Visit poster's website 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1678
Location: Japan

PostPosted: Thu 16 Aug 2018, 01:33    Post subject:  

grepless:
Code:
read x x x x x x TOTAL x FREE x <<< $(free -m)
Back to top
View user's profile Send private message 
fredx181


Joined: 11 Dec 2013
Posts: 3538
Location: holland

PostPosted: Thu 16 Aug 2018, 01:40    Post subject:  

MochiMoppel wrote:
Another way:
Code:
FREERESULT=(`free -m`)
TOTAL=${FREERESULT[6]}
FREE=${FREERESULT[8]}


That doesn't work with newer version
Code:
# free --version
free from procps-ng 3.3.12


Output is different from older:
Code:
# free -m
              total        used        free      shared  buff/cache   available
Mem:           2011         301         852         114         857        1376
Swap:          3184           0        3184


Fred

_________________
Dog Linux website
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 12973
Location: Gatineau (Qc), Canada

PostPosted: Thu 16 Aug 2018, 01:41    Post subject:  

Hello *RSH.
Code:
free -m | awk '$1 ~ /Mem/ { print $2,$4 }'
This way you get only the desired results.
You can change the , to "\t" if you wish the line to "breathe" more.

Examples:
Code:
free -m | awk '$1 ~ /Mem/ { print $2,$4 }'
3914 2630

free -m | awk '$1 ~ /Mem/ { print $2"\t"$4 }'
3914   2593

Awk is totally secure, BTW. Unused data is discarded to /dev/null, as I
understand it.

IHTH

_________________
musher0
~~~~~~~~~~
Je suis né pour aimer et non pas pour haïr. (Sophocle) /
I was born to love and not to hate. (Sophocles)

Last edited by musher0 on Thu 16 Aug 2018, 01:52; edited 1 time in total
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1678
Location: Japan

PostPosted: Thu 16 Aug 2018, 01:47    Post subject:  

fredx181 wrote:
That doesn't work with newer version
Code:
# free --version
free from procps-ng 3.3.12
In my puppy free is linked to busybox

This should work in any version:
Code:
FREERESULT=(`free -m|grep Mem:`)
TOTAL=${FREERESULT[1]}
FREE=${FREERESULT[3]}


.

Last edited by MochiMoppel on Thu 16 Aug 2018, 02:13; edited 1 time in total
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 12973
Location: Gatineau (Qc), Canada

PostPosted: Thu 16 Aug 2018, 01:56    Post subject:  

To make it a bit more informative:
Code:
echo -e "total\tfree";free -m | awk '$1 ~ /Mem/ { print $2"\t"$4 }'
total   free
3914   2568

_________________
musher0
~~~~~~~~~~
Je suis né pour aimer et non pas pour haïr. (Sophocle) /
I was born to love and not to hate. (Sophocles)
Back to top
View user's profile Send private message 
fredx181


Joined: 11 Dec 2013
Posts: 3538
Location: holland

PostPosted: Thu 16 Aug 2018, 02:02    Post subject:  

MochiMoppel wrote:
In my puppy free is linked to busybox

Ah, yes, probably the case in all puppies, so my post above can be ignored.

_________________
Dog Linux website
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1678
Location: Japan

PostPosted: Thu 16 Aug 2018, 02:17    Post subject:  

fredx181 wrote:
Ah, yes, probably the case in all puppies, so my post above can be ignored.
So probably my edit can be ignored too Laughing
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 12973
Location: Gatineau (Qc), Canada

PostPosted: Thu 16 Aug 2018, 02:18    Post subject:  

Hi *RSH.

Careful, it's a memory clean-up script! It's not exactly what you are looking
for, but there may be some code you wish to plunder Wink in here:
http://murga-linux.com/puppy/viewtopic.php?p=831720&sort=lastpost#831720

It's derived from Joe Arose's original, way back when, with add-ons /
suggestions from various forum members. It may save you some time,
who knows.

IHTH.

_________________
musher0
~~~~~~~~~~
Je suis né pour aimer et non pas pour haïr. (Sophocle) /
I was born to love and not to hate. (Sophocles)
Back to top
View user's profile Send private message 
ITSMERSH


Joined: 02 May 2018
Posts: 458

PostPosted: Thu 16 Aug 2018, 03:21    Post subject:  

Wow, thanks for all the replies! Smile

A lot to check out...

_________________
RSH
Back to top
View user's profile Send private message 
rockedge


Joined: 11 Apr 2012
Posts: 853
Location: Connecticut, United States

PostPosted: Thu 16 Aug 2018, 10:53    Post subject:  

MochiMoppel I took your code snip and using it as an icon on the desktop

Code:
#!/bin/sh
FREERESULT=(`free -h|grep Mem:`)
TOTAL=${FREERESULT[1]}
FREE=${FREERESULT[3]}
gxmessage "Total->$TOTAL  Free->$FREE"
Back to top
View user's profile Send private message Visit poster's website 
musher0

Joined: 04 Jan 2009
Posts: 12973
Location: Gatineau (Qc), Canada

PostPosted: Thu 16 Aug 2018, 15:25    Post subject:  

@*RSH: You're welcome!
_________________
musher0
~~~~~~~~~~
Je suis né pour aimer et non pas pour haïr. (Sophocle) /
I was born to love and not to hate. (Sophocles)
Back to top
View user's profile Send private message 
ITSMERSH


Joined: 02 May 2018
Posts: 458

PostPosted: Tue 06 Nov 2018, 17:03    Post subject: Understanding AWK (awk)
Subject description: Thanks
 

musher0 wrote:
Code:
free -m | awk '$1 ~ /Mem/ { print $2,$4 }'

Today I was in the need to get specific results from --> uptime <-- to show some statistics in a gui. The output of --> uptime <-- varies dependent on how long the computer is running.

So I wrote a small line to get the desired information.
Code:
UPTIME="`uptime | cut -d ' ' -f 4 | cut -d ',' -f 1` hours"

But after the computer was running for more than 24 hours this line returned just --> 1 <-- instead of e.g. 25:43.

So I was in the need to modify the above line of code.

Thanks to the coded line above by musher0 it seems I'm going to understand --> awk <-- at least a little. So, here's how I extracted that desired information when the computer is running for more than 24 hours.
Code:
# Get up-time of running System
UPTIME="`uptime`"; echo "$UPTIME" > /tmp/upt;
if (grep 'day' /tmp/upt >/dev/null) then
   UPTIME1=`uptime | awk '$2 ~ /up/ { print $3,$4,$5 }' | cut -d ',' -f 1`
   UPTIME2=`uptime | awk '$2 ~ /up/ { print $3,$4,$5 }' | cut -d ',' -f 2`
   UPTIME="$UPTIME1, $UPTIME2 hours"
   else
   UPTIME="`uptime | cut -d ' ' -f 4 | cut -d ',' -f 1` hours"
fi

Of course, I'm sure there's some smarter solution by the experts here. Though, I'm somehow glad to have understood a bit more of those special functions.

Thank you, musher0 ! Very Happy

_________________
RSH
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 3 [45 Posts]   Goto page: 1, 2, 3 Next
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.6114s ][ Queries: 12 (0.1049s) ][ GZIP on ]