I am new to using bash. Now I am about to read a value, but the output of the console is too long and I just want to shorten it to the specific value.
netstat -m
24270/3315/27585 mbufs in use (current/cache/total)
4142/1724/5866/1000000 mbuf clusters in use (current/cache/total/max)
40/1478 mbuf+clusters out of packet secondary zone in use (current/cache)
0/145/145/524288 4k (page size) jumbo clusters in use (current/cache/total/max)
0/0/0/524288 9k jumbo clusters in use (current/cache/total/max)
0/0/0/83968 16k jumbo clusters in use (current/cache/total/max)
...
Now I want to get to the 5866 in the second line and wrap it in a variable. Currently my script looks like this:
mbuf_stat=$(netstat -m)
mbuf=$mbuf_stat
mbuf=${mbuf#*)}
mbuf=${mbuf#*/}
mbuf=${mbuf#*/}
mbuf=${mbuf%%/*}
echo "$mbuf"
Is there an easier way to do this? It seems pretty complicated to me. Unfortunately, I have not found a simpler way yet.
bashregular expressions to do this kind of parsing, but remember that the primary purpose ofbashis to run other programs to do your actual data processing, not do the processing itself.netstatin this case) may have some command-line option to provide the output in more parsable/precise manner. My comment may not be relevant in the case ofnetstat, but I am just putting it out here as a note since you are new to shell scripting. :-)