Need to just grep a value and not the unit

kdean

New Member
Joined
Jan 17, 2020
Messages
5
Reaction score
0
Credits
0
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.
 


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}'
 
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.
 
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
 
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

I've just always used and relied on awk lol :) I do wish I knew cut a little better though, planning on getting it down.
 
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.
 

Staff online

Members online


Latest posts

Top