Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Output filtering
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Output filtering

edited June 2014 in Help

It has been a long time and I am not really into advanced filtering output of Linux commands with grep, awk and other tools.

What I want to do is filter out used and free RAM of free -m.

My command so far:
free -m | grep - | awk -F : '{print $2}'

Output example:
14 1009

14 is used and 1009 is free.

Now I need to split this output into two commands so one gives out the used amount and the other the free amount.

Been trying stuff with cut and so on but the problem is that the example output is $2 in whole so the whole thing gets cut out and the output is empty. Not that it would have been easier if free -m would have put $2 in for used and $3 for free...

So how do change my filter command into two different to filter out the amount of used and free memory?

Comments

  • blackblack Member
    free -m | grep - | cut -d ":" -f2 | tr -s " " | sed "s/^ //"
  • edited June 2014

    Do you even test your commands?

    I get the same output from it as from mine.

    :~# free -m | grep - | cut -d ":" -f2 | tr -s " " | sed "s/^ //"
    14 1009
    
  • @NekoShiinachan

    $ echo "14 1009" | cut -d" " -f1
    14
    $ echo "14 1009" | cut -d" " -f2
    1009
    Thanked by 1NekoShiinachan
  • So basically I could do cram=$(free -m | grep - | awk -F : '{print $2}') and then do freeram=$(echo $cram | cut -d" " -f1) and usedram=$(echo $cram | cut -d" " -f2) ?

  • blackblack Member

    @NekoShiinachan said:
    Do you even test your commands?

    I get the same output from it as from mine.

    > :~# free -m | grep - | cut -d ":" -f2 | tr -s " " | sed "s/^ //"
    > 14 1009
    > 

    Isn't that what you wanted?

    Output example: 14 1009

  • raw=`free -m | grep - | awk -F : '{print $2}'` && used=`echo $raw | awk '{ print $1 }'` && free=`echo $raw | awk '{ print $2 }'` && echo -e "Used : $used\nFree : $free"
    Thanked by 1NekoShiinachan
  • @NekoShiinachan said:
    So basically I could do cram=$(free -m | grep - | awk -F : '{print $2}') and then do freeram=$(echo $cram | cut -d" " -f1) and usedram=$(echo $cram | cut -d" " -f2) ?

    Exactly.

  • perennateperennate Member, Host Rep
    edited June 2014
    # echo 14
    14
    # echo 1009
    1009

    Edit: or you could do

    # free -m | grep - | awk -F : '{print $2}' | echo 14
    14
    # free -m | grep - | awk -F : '{print $2}' | echo 1009
    1009

    Edit: or you coudl get more memory with:

    # free -m | grep - | awk -F : '{print $2}' | echo 14
    14
    # free -m | grep - | awk -F : '{print $2}' | echo 100000009
    100000009
  • edited June 2014

    black said: Isn't that what you wanted?

    No, that was the output of my command.

    Should have described it better.

    @perennate troll somewhere else

  • blackblack Member

    @NekoShiinachan said:
    No, that was the output of my command.

    The output of your command had a lot more spaces than the output of my command, which is why you wouldn't be able to pass it as $2 $3 as arguments to another program. Anyway, hopefully socials solved your problem.

  • @black

    Yes, that was the problem because the output that was generated by filtering is $2 of "free -m" while "-/+ buffers/cache:" is $1 of the filtered "free -m".

    And the because the output "14 1009" was the whole $2 the cutting failed because you always cut away the whole thing because the whole output is $2.

This discussion has been closed.