如何根据第6场内容grep线

时间:2015-05-27 06:31:15

标签: linux bash awk sed grep

我想知道如何根据内存使用过滤行:我希望只能找到(RES内存使用量)超过5GB的PID

明确地说,我有以下几行

20697 user    20   0  357m  24m  19m S 19.0  0.1 887:15.00 pulseaudio
13017 user    20   0 8381m 3.7g  35m S 127.7  7.9 803:11.96 MATLAB
12654 user    20   0 11.2g 6.1g  10m S 104.8 13.0 784:41.07 MATLAB
3088 user    20   0 17.4g  12g  11m S 101.0 26.3   2043:48 MATLAB
12548 user    20   0 8797m 3.5g  13m S 101.0  7.4 802:21.00 MATLAB
12785 user    20   0 8543m 3.6g  35m S 101.0  7.6 804:36.81 MATLAB
12909 user    20   0 12.1g 5.5g  28m S 99.1 11.8 796:01.45 MATLAB
13154 user    20   0 10.1g 5.6g  10m S 99.1 11.8 797:07.14 MATLAB

我只想看第6个字段大于5 GB的行

编辑:请注意,排序并不重要。只选择超过阈值的行很重要。

谢谢!

5 个答案:

答案 0 :(得分:3)

使用gawk

$ cat n.txt
20697 user    20   0  357m  24m  19m S 19.0  0.1 887:15.00 pulseaudio
13017 user    20   0 8381m 3.7g  35m S 127.7  7.9 803:11.96 MATLAB
12654 user    20   0 11.2g 6.1g  10m S 104.8 13.0 784:41.07 MATLAB
3088 user    20   0 17.4g  12g  11m S 101.0 26.3   2043:48 MATLAB
12548 user    20   0 8797m 3.5g  13m S 101.0  7.4 802:21.00 MATLAB
12785 user    20   0 8543m 3.6g  35m S 101.0  7.6 804:36.81 MATLAB
12909 user    20   0 12.1g 5.5g  28m S 99.1 11.8 796:01.45 MATLAB
13154 user    20   0 10.1g 5.6g  10m S 99.1 11.8 797:07.14 MATLAB

$ gawk '
BEGIN{
    k=1024;
    m["k"]=k;
    m["m"]=k*k;
    m["g"]=k*k*k;
}
{
    patsplit($6, a, "([0-9.]*)|([kmg])");
    value=a[1]*m[a[2]];
    if (value>5*k*k*k) print;
}' n.txt 

12654 user    20   0 11.2g 6.1g  10m S 104.8 13.0 784:41.07 MATLAB
3088 user    20   0 17.4g  12g  11m S 101.0 26.3   2043:48 MATLAB
12909 user    20   0 12g 5.5g  28m S 99.1 11.8 796:01.45 MATLAB
13154 user    20   0 10.1g 5.6g  10m S 99.1 11.8 797:07.14 MATLAB

更优化(和更不通用)的版本:

$ awk '$6 ~ /^[0-9.]*g$/{if((0 + $6)>5) print}' n.txt

答案 1 :(得分:3)

$ awk '$6~/g/ && ($6+0)>5' file
12654 user    20   0 11.2g 6.1g  10m S 104.8 13.0 784:41.07 MATLAB
3088 user    20   0 17.4g  12g  11m S 101.0 26.3   2043:48 MATLAB
12909 user    20   0 12.1g 5.5g  28m S 99.1 11.8 796:01.45 MATLAB
13154 user    20   0 10.1g 5.6g  10m S 99.1 11.8 797:07.14 MATLAB

答案 2 :(得分:1)

如果你不介意使用awk

$ cat n.txt
20697 user    20   0  357m  24m  19m S 19.0  0.1 887:15.00 pulseaudio
13017 user    20   0 8381m 3.7g  35m S 127.7  7.9 803:11.96 MATLAB
12654 user    20   0 11.2g 6.1g  10m S 104.8 13.0 784:41.07 MATLAB
3088 user    20   0 17.4g  12g  11m S 101.0 26.3   2043:48 MATLAB
12548 user    20   0 8797m 3.5g  13m S 101.0  7.4 802:21.00 MATLAB
12785 user    20   0 8543m 3.6g  35m S 101.0  7.6 804:36.81 MATLAB
12909 user    20   0 12.1g 5.5g  28m S 99.1 11.8 796:01.45 MATLAB
13154 user    20   0 10.1g 5.6g  10m S 99.1 11.8 797:07.14 MATLAB

$ awk '{if($6>=5) {print}}' n.txt
12654 user    20   0 11.2g 6.1g  10m S 104.8 13.0 784:41.07 MATLAB
12909 user    20   0 12.1g 5.5g  28m S 99.1 11.8 796:01.45 MATLAB
13154 user    20   0 10.1g 5.6g  10m S 99.1 11.8 797:07.14 MATLAB

答案 3 :(得分:0)

YourStream | grep -E -e '^([[:space:]]*[^[:space:]]{1,}){5}[[:space:]]*([1-9][0-9]|[5-9])(.[0-9]*){0,1}g' 
  • 使用您的流输入和grep。
  • 订购或其他行动更喜欢awk

答案 4 :(得分:-2)

您不能使用命令行中的排序选项,但可以使用变通方法来完成此操作。

  • 开始顶部(正常模式),
  • 设置顺序(按内存顺序:O n),
  • 写配置文件(W),
  • 退出顶部