So legen Sie mehrere Leerzeichen (z. B. Tabs) als Trennzeichen in bash `cut` fest

3704
Idlecool

Ich möchte die CPU-Nutzung / freien Prozentsatz von der mpstat-Ausgabe abrufen. Die Bash cutkann verwendet werden, um solche Details abzurufen, aber ich weiß nicht, was das Trennzeichen sein soll.

[idlecool @ archbitch proc] $ mpstat | grep "alle" | schneide -d '$ x' -f11

was sollte $ x sein, damit ich Leerzeichen überspringen und einen Wert auswählen kann, der% idle entspricht?

Ausgabe von mpstat:

[idlecool@archbitch proc]$ mpstat  Linux 2.6.36-ARCH (archbitch) 01/14/11 _i686_ (2 CPU)  19:58:53 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle 19:58:53 all 5.51 0.01 2.96 0.84 0.00 0.01 0.00 0.00 90.66 
0

2 Antworten auf die Frage

5
vilpan

The delimiter should be space. However, mpstat uses multiple spaces between printed fields for alignment. Therefore you also need tr to squeeze multiple delimiters.

mpstat | grep -F all | tr -s ' ' | cut -d ' ' -f 11 

Note: -F flag for grep is not essential in this case, though, I habitually use it whenever the pattern is not a regex as it significantly speeds up searches on big inputs.

2
Ignacio Vazquez-Abrams

Falsches Werkzeug.

mpstat | awk '$2 == "all" { print $11 }'