如何从命令输出中删除空格/TAB

编程入门 行业动态 更新时间:2024-10-19 20:24:19
本文介绍了如何从命令输出中删除空格/TAB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

请针对以下理解问题提出建议?

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

本文发布于:2023-11-02 14:34:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552662.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:空格   命令   TAB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!