andymrg
andymrg

Reputation: 204

How Extract columns to free command

I execute the command free -mo in linux and my output is:

             total       used       free     shared    buffers     cached
Mem:         64401      33265      31135          0        667      15455
Swap:         4099          0       4099

But i need the output:

             total       used       free
Mem:         64401      33265      31135
Swap:         4099          0       4099

I intended to use free -mo | awk '{print $1,$2,$3,$4}' but my result is:

total used free shared
Mem: 64401 33268 31132
Swap: 4099 0 4099

I appreciate your help .

Upvotes: 2

Views: 2897

Answers (3)

anubhava
anubhava

Reputation: 784998

You can use:

free -mo |
awk '{print (NR==1?"Type":""), $1, $2, $3, (NR==1?"":$4)}' |
column -t
  • Added a columnType` for make first row a consists of 4 column
  • column -t is for tabular formatting

Output:

Type   total  used  free
Mem:   2003   163   1840
Swap:  1430   0     1430

Upvotes: 3

tink
tink

Reputation: 15204

Preserving right aligned numbers:

free -mo | awk 'BEGIN{len=0} NR==1{len=length(gensub(/free.*$/, "free",1))} {print substr($0,1,len)}'
             total       used       free
Mem:          7913       7377        535
Swap:         8115        569       7546

Upvotes: 1

karakfa
karakfa

Reputation: 67467

another awk without filler header

$ awk -v OFS='\t' 'NR==1{print "",$1,$2,$3;next} {print $1,$2,$3,$4}' file

        total   used    free
Mem:    64401   33265   31135
Swap:   4099    0       4099

Upvotes: 1

Related Questions