grep issue

The below command is not working

stackmem="$(pmap $1 | grep -i '[ stack ]' | awk '{print $2}'|  tr -d ' K')"

I need to grep strictly for ----> [ stack ]

Regards,
Mohtashim

[ is a special character in grep and most other regular expressions so you need to do it like \[ stack \]

I don't think field 2 is what you want since awk splits on any whitespace.

And since you're using awk anyway, why not do it all in awk instead of using three pipes? awk's a fairly complete language and running it to do one thing on one line is severe overkill.

$ echo '[ stack ] 99K' | awk ' /\[ stack \]/ {sub(/[A-Z]/, "", $4); print $4; }'
99
$
grep -i '\[ stack \]' 

Btw. That "tr" command may be iffy.

echo "Koala Bears are Killers" | tr -d ' K'
oalaBearsareillers
1 Like