Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to grep lines according to 6th field contents

I would like to know how to filter lines according to the mem usage: I would like to be able to find only the PIDs that (RES mem usage) exceed 5GB

Explicitly, I have the following lines

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

and I'd like to grep only the lines where the 6th field is larger than 5 GB

EDIT: Note that sorting is not important. Just selecting the lines that exceed the threshold is important.

Thanks!

like image 457
Yuval Atzmon Avatar asked Nov 22 '25 05:11

Yuval Atzmon


2 Answers

$ 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
like image 91
Ed Morton Avatar answered Nov 24 '25 17:11

Ed Morton


Using 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

A more optimized (& less generic) version:

$ awk '$6 ~ /^[0-9.]*g$/{if((0 + $6)>5) print}' n.txt
like image 31
anishsane Avatar answered Nov 24 '25 17:11

anishsane



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!