0

i need help. i have this file i want read it line by line with for loops and delete the line less than 20% the number will with list %CPU .

%CPU   PID USER       UID COMMAND         %MEM
71  9136 ashti     1000 firefox          4.6
36  1432 ashti     1000 gnome-shell      8.6
25 9100 ashti     1000 gedit            1.1
 3  9092 ashti     1000 nautilus         1.2
 2  9109 ashti     1000 gnome-terminal-  0.9
 1 1248 ashti     1000 Xorg             2.6
 0  1375 ashti     1000 VBoxClient       0.0
 0  9118 ashti     1000 bash             0.1
 0   269 root         0 systemd-journal  2.8

i want be like this example

%CPU   PID USER       UID COMMAND         %MEM
71  9136 ashti     1000 firefox          4.6
36  1432 ashti     1000 gnome-shell      8.6
25 9100 ashti     1000 gedit            1.1

and save in new file .

best regards

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
kawa
  • 1

3 Answers3

2

With grep:

grep -e "^%" -e "^[2-9][1-9]" file | column -t

column -t to make it pretty:

%CPU  PID   USER   UID   COMMAND      %MEM
71    9136  ashti  1000  firefox      4.6
36    1432  ashti  1000  gnome-shell  8.6
25    9100  ashti  1000  gedit        1.1
1

With retaining header, awk expression:

$ awk 'NR==1 || (NR > 1 && $1 > 20)' file
%CPU   PID USER       UID COMMAND         %MEM
71  9136 ashti     1000 firefox          4.6
36  1432 ashti     1000 gnome-shell      8.6
25 9100 ashti     1000 gedit            1.1
  • please check this link its my code question

    [link] (https://unix.stackexchange.com/questions/549368/i-need-help-i-have-this-code-i-want-add-if-the-number-of-c-grater-than-2-pri)

    – kawa Oct 29 '19 at 20:01
0

A for loop is not a good choice for this task - for a discussion of the reasons why, see for example

My personal choice would be Miller:

$ mlr --pprint filter -x '${%CPU} < 20' file > newfile

$ cat newfile
%CPU PID  USER  UID  COMMAND     %MEM
71   9136 ashti 1000 firefox     4.6
36   1432 ashti 1000 gnome-shell 8.6
25   9100 ashti 1000 gedit       1.1
steeldriver
  • 81,074
  • please check this link its my code question

    [link] (https://unix.stackexchange.com/questions/549368/i-need-help-i-have-this-code-i-want-add-if-the-number-of-c-grater-than-2-pri)

    – kawa Oct 29 '19 at 20:01