Jeremy
Jeremy

Reputation: 13

Trying to use awk to print a column but says print not found

So I am trying to do some homework and am having a lot of issues. This is the first script I have written in UNIX/Linux. Right now I am trying to print out a few columns from the free command and use awk to just grab those columns I need. Every time I try to do this, i get a bash: print command not found.

Command I am trying to use:

free | awk `{print $3}`

I have also tried without the {}.

So when It is finished I would like it to say for example: Total Free Memory: xx MB with the xx filled in of course with the result.

Any help is appreciated. Thanks.

UPDATE: Okay so don't use the backticks to get that to work. So now I would like the whole command to end up saying: Total Free Memory: xx MB

The command I have so far is:

echo "Current Free Memory: " | free -m | awk '{print $3}'

I only want just that one column row intersection.

Upvotes: 1

Views: 2944

Answers (3)

davidedb
davidedb

Reputation: 876

free -m | awk '/Mem:/ {print "Current Free Memory: " $4 " MB"}'

Upvotes: 0

Yam Gotham
Yam Gotham

Reputation: 128

You should go for the field 4. As I suggest using awk and sed but it is up to you.

Here is my suggestion :

freem="$(free -m | awk '{print $4}' | sed -n '2p')"

echo "Total Free Memory: $freem MB"

Where $4 is the columns you want and 2p is the line you want.

Before writing the script, check your result in command line.

Upvotes: 1

fedorqui
fedorqui

Reputation: 290025

You are saying

free | awk `{print $3}`

Whereas you need to say awk '{...}'. That is, use single quotes instead of backticks:

free | awk '{print $3}'
           ^          ^

Note this is explained in man awk:

Syntax

  awk <options> 'Program' Input-File1 Input-File2 ...

  awk -f PROGRAM-FILE <options> Input-File1 Input-File2 ...

Upvotes: 4

Related Questions