jackorer
jackorer

Reputation: 11

Linux: Use cut command to remove everything before AND after certain characters

The question is to show how many NMI's have occured. If I check "cat /proc/interrupts" it says

NMI:          0          0   Non-maskable interrupts

Now I need a one liner that returns just the 2 0's. I have done:

grep -P 'NMI' /proc/interrupts | cut -d ':' -f2

to remove the "NMI:" in the beginning but no idea how to do all the rest in the same line.

Cheers

Upvotes: 1

Views: 6667

Answers (3)

user539810
user539810

Reputation:

For this, my weapon of choice would be awk since it was designed with this sort of processing in mind:

awk '/^NMI/{
    for(i=1; i<=NF; ++i)
        if($i~/^[[:digit:]]+$/)
            printf("%10d ", $i);
    print ""
}' /proc/interrupts

This should work for any number of (virtual) CPUs in the event that the CPU count increases. My /proc/interrupts file has 4 numeric fields for example.

Upvotes: 1

fedorqui
fedorqui

Reputation: 289545

You can use awk for example:

$ awk '/^NMI/ {print $2, $3}' /proc/interrupts
0 0

If you still want to use cut, then you firstly have to squeeze the spaces with tr:

$ grep "^NMI:" /proc/interrupts | tr -s ' ' | cut -d' ' -f2,3
0 0

Upvotes: 4

Gilles Qu&#233;not
Gilles Qu&#233;not

Reputation: 185025

With :

echo $(grep -oP 'NMI:\s+\K\d+\s+\d+' /proc/interrupts)

Upvotes: 1

Related Questions