awk & remove other than numbers

lsps -s | awk '{print $1}'|tail -1    
71680 MB
lsps -s | awk '{print $2}'|tail -1
2%
vmstat |grep lcpu |awk '{print $3}'
lcpu=8

I need to removt the MB % lcpu= from the outputs so they can show as

71680
2
8

respectively.

Please advise.

Thank you so much,

Try this Code,

nawk '{while(sub(/[a-zA-Z=%]/,"")){}print}' file_name

you can change as many special character do you want.

Cheers,
Ranga:)

1 Like

why exactly do you need a while and a sub?
Won't gsub be sufficient?
also:

echo '2%' | nawk '{print $0+0}'
echo '2%' | tr -cd '[0-9]'
1 Like

why not, you can use this too:)

nawk '{gsub(/[a-zA-Z=%]/,"");print}' file_name

Cheers,
Ranga :slight_smile:

1 Like

It works fine for lsps -s output.

but, it is a bit tricky for vmstat |grep lcpu

The output of

vmstat |grep lcpu

is

System configuration: lcpu=8 mem=20480MB

How do you get 8 (which is lcpu value)?

Thank you so much.

vmstat | nawk -F= '/lcpu/ {print $2+0}'
1 Like

Thank you so much. It works perfect!

---------- Post updated at 11:12 AM ---------- Previous update was at 11:01 AM ----------

Sorry, one more thing to ask!

System configuration: lcpu=8 mem=20480MB  

for the mem,
I tried,

vmstat | nawk -F= '/mem/ {print $3+0}'
20480
0

but it gives extra line with 0.

How do you get only 20480?

Thank you so much

what does your vmstat | grep mem look like?
Try:

vmstat | nawk -F= '/mem=/ {print $3+0}' 
1 Like

Yes, that is it! = (in /mem=/) took care of it.