0

I just wanted to extract VmRSS value, but the print should not show leading whitespace

renv@svr-ubt20-004:~$ cat /proc/607440/status | grep VmRSS
VmRSS:     20452 kB

Tried tr and cut commands and got below results.

renv@svr-ubt20-004:~$ cat /proc/607440/status | grep VmRSS | cut -d ':' -f 2 | tr -s " "
         20452 kB

Expectation:
20452 KB

Needed help to achieve this.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • 1
    The character between : and number in that 'file' is tab not space. Even if you 'squeeze' it, one tab still displays as 8 spaces (on common terminal emulations). You need to delete it, which tr -d could do, but (like awk) sed can do the whole job at once: sed -n 's/VmRSS:\t//p' /proc/N/status – dave_thompson_085 Jun 18 '23 at 22:19

3 Answers3

2

AWK can take care of all this:

awk '/VmRSS/ { print $2" "$3 }' /proc/607440/status
Stephen Kitt
  • 434,908
2

Try either of these:

sed -n 's/^VmRSS:\t//p' /proc/607440/status

awk 'sub(/^VmRSS:\t/,"")' /proc/607440/status

awk -F':\t' '$1=="VmRSS"{print $2}' /proc/607440/status

I'm assuming above that that's a tab after the : in your sample input since your tr -s " " didn't compress it. If that assumption is wrong then using POSIX tools:

sed -n 's/^VmRSS:[[:space:]]*//p' /proc/607440/status

awk 'sub(/^VmRSS:[[:space:]]*/,"")' /proc/607440/status

awk -F':[[:space:]]+' '$1=="VmRSS"{print $2}' /proc/607440/status

Ed Morton
  • 31,617
0

Perl:

~$ zsh
~% printf 'VmRSS:\t20452 kB' | perl -lpe 's/^VmRSS\:\t//;'
20452 kB

~% bash ~$ printf 'VmRSS:\t20452 kB' | perl -lpe 's/^VmRSS:\t//;' 20452 kB


Raku:

~$ zsh
~% printf 'VmRSS:\t20452 kB' | raku -pe 's/^VmRSS\:\t//;'
20452 kB

~% bash ~$ printf 'VmRSS:\t20452 kB' | raku -pe 's/^VmRSS:\t//;' 20452 kB

Also for Raku, you have various trim, trim-leading, and/or trim-trailing functions built-in. For those times when you just need to get rid of whitespace and don't care if it's spaces or tabs, etc.:

~$ zsh
~% printf 'VmRSS:\t20452 kB' | raku -pe 's/^VmRSS\://;' | raku -ne '.trim-leading.put;'
20452 kB

~% bash ~$ printf 'VmRSS:\t20452 kB' | raku -pe 's/^VmRSS://;' | raku -ne '.trim-leading.put;' 20452 kB


MacOS Ventura 13.4

zsh 5.9 (x86_64-apple-darwin22.0)

GNU bash, version 5.1.8(1)-release (x86_64-apple-darwin15.6.0)

jubilatious1
  • 3,195
  • 8
  • 17