Skip to content

Commit 8e5894f

Browse files
authored
Merge pull request #472 from erikw/fix/467/remove_gnu_grep_dep
Eliminate GNU Grep in cpu.sh
2 parents 7859e14 + 0ddc742 commit 8e5894f

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

segments/cpu.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
run_segment() {
55
if shell_is_linux; then
66
cpu_line=$(top -b -n 1 | grep "Cpu(s)")
7-
cpu_user=$(echo "$cpu_line" | grep -Po "(\d+(.\d+)?)(?=%?\s?(us(er)?))")
8-
cpu_system=$(echo "$cpu_line" | grep -Po "(\d+(.\d+)?)(?=%?\s?(sys?))")
9-
cpu_idle=$(echo "$cpu_line" | grep -Po "(\d+(.\d+)?)(?=%?\s?(id(le)?))")
7+
# cpu_line example: %Cpu(s): 13.3 us, 13.3 sy, 0.0 ni, 73.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
8+
cpu_user=$(echo "$cpu_line" | grep -o "[0-9]\+\(.[0-9]\+\)\? *us\(er\)\?" | awk '{ print $1 }')
9+
cpu_system=$(echo "$cpu_line" | grep -o "[0-9]\+\(.[0-9]\+\)\? *sys\?" | awk '{ print $1 }')
10+
cpu_idle=$(echo "$cpu_line" | grep -o "[0-9]\+\(.[0-9]\+\)\? *id\(le\)\?" | awk '{ print $1 }')
1011
elif shell_is_macos; then
1112
cpus_line=$(top -e -l 1 | grep "CPU usage:" | sed 's/CPU usage: //')
1213
cpu_user=$(echo "$cpus_line" | awk '{print $1}' | sed 's/%//')

0 commit comments

Comments
 (0)