• Rob's side project: I recently started Gun-Forums.com and we currently have a $100 raffle - if you're into guns, join up and enter the raffle to win $100.00. Raffle Link

Need to just grep a value and not the unit

kdean

New Member
when i run:

Code:
grep SwapCached /proc/meminfo | cut -f 2 -d ':'
I receive the following output:

30720 kB

How do I remove the kB so I may point the output to our monitoring system, OpManager. I tried to tail -n, I tried awking out. But theres something I am not doing correctly with my syntax.
 


Rob

Administrator
Staff member
Hey there kdean, weldome to Linux.org :)

First of all, do you understand what you're doing with the command above? w/o the pipe and cut you'll get the full line, so cut is what is picking out the column to show you..

the -f is telling which field to show and the -d is showing the delimiter to use (:).

Besides -f, you can use other options in cut to show like -b (bytes) or -c (characters)..

Info:
https://linuxize.com/post/linux-cut-command/

What i'd use is awk though, printing the 2nd column like:
Code:
grep SwapCached /proc/meminfo|awk '{print $2}'
 

kdean

New Member
Thank you Rob, your explanation makes sense. I was completely overthinking it. I thought I could cut just the number. So using awk would actually make way more sense. Are there any examples of parsing data where you would use the awk and cut command together? I have a basic understanding of the fundamentals of scripting just not the practical use of it. So this is really helping me connect the dots. I am not being efficient and am probably making too many system calls or something.
 

dos2unix

Well-Known Member
Rob's explanation is great, I know that works.

But you could use 'cut' if you wanted to.

grep SwapCached /proc/meminfo | cut -f2 -d: | cut -f1 -dk
 

kdean

New Member
Rob's explanation is great, I know that works.

But you could use 'cut' if you wanted to.

grep SwapCached /proc/meminfo | cut -f2 -d: | cut -f1 -dk
Thanks dos2unix. This makes sense, I had to a do a 2nd cut. Now why did you not put apostrophes around the : and the k ? I would have assumed this would cause a syntax error but it works like a charm.
 


Members online


Top