如何从命令输出中删除空格/TAB [英] How to remove space/TAB from command output
问题描述
请针对以下理解问题提出建议?
Please advice about the following understanding problem ???
我在Linux机器上键入以下命令:(为了通过top命令获得总内存)
I type on my linux machine this: ( in order to get the total memory by top command )
top -n1 | grep Mem:
Mem: 2075024k total, 2059064k used, 15960k free, 249212k buffers
所以现在我想获取第二个字段中存在的总内存,所以我这样做:
so now I want to get the total memory that exists in the second field so I do this:
# top -n1 | grep Mem: | awk '{print $2}'
但没有任何结果!-:(
但是,如果我这样做:(第三个字段),则尽管该值存在于 second 字段中?
but if I do this: ( the third field ) , then I get the total value in spite the value exists in the second field ??
# top -n1 | grep Mem: | awk '{print $3}'
2075024k
我想以这种方式使用普通命令的任何方式:(awk'{print $ 2}')
any way I want to use the normal command this: ( awk '{print $2}' )
top -n1 | grep Mem: | awk '{print $2}'
请告知需要在正常"命令中添加哪些内容以获取第二个值- 2075024k -此处出了什么问题????
please advice what need to add to the "normal" command in order to get the second value - 2075024k - what’s wrong here ????
# top -n1 | grep Mem: | od -c
0000000 M e m : 033 ( B 033 [ m 033 [ 3 9 ;
0000020 4 9 m 033 ( B 033 [ m 2 0 7 5 0
0000040 2 4 k 033 ( B 033 [ m 033 [ 3 9 ; 4
0000060 9 m t o t a l , 033 ( B 033 [ m 033 [
0000100 3 9 ; 4 9 m 033 ( B 033 [ m 2 0
0000120 5 8 6 5 6 k 033 ( B 033 [ m 033 [ 3
0000140 9 ; 4 9 m u s e d , 033 ( B 033 [ m
0000160 033 [ 3 9 ; 4 9 m 033 ( B 033 [ m
0000200 1 6 3 6 8 k 033 ( B 033 [ m 033
0000220 [ 3 9 ; 4 9 m f r e e , 033 ( B 033
0000240 [ m 033 [ 3 9 ; 4 9 m 033 ( B 033 [ m
0000260 2 5 0 5 0 0 k 033 ( B 033 [
0000300 m 033 [ 3 9 ; 4 9 m b u f f e r s
0000320 033 ( B 033 [ m 033 [ 3 9 ; 4 9 m 033 [
0000340 K \n
0000342
推荐答案
awk '/^MemTotal:/ {print $2}' /proc/meminfo
输出(例如),单位:kB:
Output (e.g.), unit: kB:
3095532
或者:
grep -oP "^MemTotal: *\K[^ ]+" /proc/meminfo
输出(例如),单位:kB:
Output (e.g.), unit: kB:
3095532
这篇关于如何从命令输出中删除空格/TAB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!