Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8283773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:51:35+00:00 2026-06-08T10:51:35+00:00

Hi I need an script to read number of eth interrupts from the /proc/interrupts

  • 0

Hi I need an script to read number of eth interrupts from the /proc/interrupts file with awk and find the total number of interrupts per CPU core.An then I want to use them in bash.The content of the file is;

      CPU0       CPU1       CPU2       CPU3

47:   33568      45958      46028      49191     PCI-MSI-edge    eth0-rx-0
48:     0          0          0          0       PCI-MSI-edge      eth0-tx-0
49:     1          0          1          0       PCI-MSI-edge      eth0
50:   28217      42237      65203      39086     PCI-MSI-edge    eth1-rx-0
51:     0          0          0          0       PCI-MSI-edge      eth1-tx-0
52:     0          1          0          1       PCI-MSI-edge      eth1
59:     114991     338765      77952     134850  PCI-MSI-edge eth4-rx-0
60:     429029     315813     710091      26714  PCI-MSI-edge eth4-tx-0
61:      5          2          1          5      PCI-MSI-edge     eth4
62:    1647083     208840    1164288     933967  PCI-MSI-edge eth5-rx-0
63:     673787    1542662     195326    1329903  PCI-MSI-edge eth5-tx-0
64:     5          6          7          4       PCI-MSI-edge      eth5

I am reading this file with awk in this code:

#!/bin/bash

 FILE="/proc/interrupts"

 output=$(awk 'NR==1 {
 core_count = NF 
 print core_count
 next
}
/eth/ {
 for (i = 2; i <= 2+core_count; i++)
 totals[i-2] += $i
}

END {
 for (i = 0; i < core_count; i++)
 printf("%d\n", totals[i])
}
' $FILE)

core_count=$(echo $output | cut -d' ' -f1)

output=$(echo $output | sed 's/^[0-9]*//')

totals=(${output// / })

In this approach, I handle the total core count and then the total interrupts for per CORE in order to sort them in my script.But I can only handle the number in totals array lke this,

    totals[0]=22222
    totals[1]=33333

But I need to handle them as tuple with the name of the CPU cores.

    totals[0]=(cPU1,2222)
    totals[1]=(CPU',3333)

I think I must assign the names to an array and read themn to bash as tuples in my SED. How can I achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T10:51:38+00:00Added an answer on June 8, 2026 at 10:51 am

    First of all, there’s no such thing as a ‘tuple’ in bash. And arrays are completely flat. It means that you either have a ‘scalar’ variable, or one level array of scalars.

    There is a number approaches to the task you’re facing. Either:

    1. If you’re using a new enough bash (4.2 AFAIR), you can use an associative array (hash, map or however you call it). Then, the CPU names will be keys and the numbers will be values;
    2. Create a plain array (perl-like hash) where odd indexes will have the keys (CPU names) and even ones — the values.
    3. Create two separate arrays, one with the CPU names and other with the values,
    4. Create just a single array, with CPU names separated from values by some symbol (i.e. = or :).

    Let’s cover approach 2 first:

    #!/bin/bash
    
    FILE="/proc/interrupts"
    
    output=$(awk 'NR==1 {
        core_count = NF
        for (i = 1; i <= core_count; i++)
            names[i-1] = $i
        next
    }
    /eth/ {
        for (i = 2; i <= 2+core_count; i++)
            totals[i-2] += $i
    }
    
    END {
        for (i = 0; i < core_count; i++)
            printf("%s %d\n", names[i], totals[i])
    }
    ' ${FILE})
    
    core_count=$(echo "${output}" | wc -l)
    totals=(${output})
    

    Note a few things I’ve changed to make the script simpler:

    1. awk now outputs `cpu-name number’, one per line, separated by a single space;
    2. the core count is not output by awk (to avoid preprocessing output) but instead deduced from number of lines in the output,
    3. the totals array is created by flattening the output — both spaces and newlines will be treated as whitespace and use to separate the values.

    The resulting array looks like:

    totals=( CPU0 12345 CPU1 23456 ) # ...
    

    To iterate over it, you could use something like (the simple way):

    set -- "${totals[@}}"
    while [[ $# -gt 0 ]]; do
        cpuname=${1}
        value=${2}
    
        # ...
    
        shift;shift
    done
    

    Now let’s modify it for approach 1:

    #!/bin/bash
    
    FILE="/proc/interrupts"
    
    output=$(awk 'NR==1 {
        core_count = NF
        for (i = 1; i <= core_count; i++)
            names[i-1] = $i
        next
    }
    /eth/ {
        for (i = 2; i <= 2+core_count; i++)
            totals[i-2] += $i
    }
    
    END {
        for (i = 0; i < core_count; i++)
            printf("[%s]=%d\n", names[i], totals[i])
    }
    ' ${FILE})
    
    core_count=$(echo "${output}" | wc -l)
    declare -A totals
    eval totals=( ${output} )
    

    Note that:

    1. the awk output format has been changed to suit the associative array semantics,
    2. totals is declared as an associative array (declare -A),
    3. sadly, eval must be used to let bash directly handle the output.

    The resulting array looks like:

    declare -A totals=( [CPU0]=12345 [CPU1]=23456 )
    

    And now you can use:

    echo ${totals[CPU0]}
    
    for cpu in "${!totals[@]}"; do
        echo "For CPU ${cpu}: ${totals[${cpu}]}"
    done
    

    The third approach can be done a number of different ways. Assuming you can allow two reads of /proc/interrupts, you could even do:

    FILE="/proc/interrupts"
    
    output=$(awk 'NR==1 {
        core_count = NF
        next
    }
    /eth/ {
        for (i = 2; i <= 2+core_count; i++)
            totals[i-2] += $i
    }
    
    END {
        for (i = 0; i < core_count; i++)
            printf("%d\n", totals[i])
    }
    ' ${FILE})
    
    core_count=$(echo "${output}" | wc -l)
    names=( $(cat /proc/interrupts | head -n 1) )
    totals=( ${output} )
    

    So, now the awk is once again only outputting the counts, and names are obtained by bash from first line of /proc/interrupts directly. Alternatively, you could create split arrays from a single array obtained in approach (2), or parsing the awk output some other way.

    The result would be in two arrays:

    names=( CPU0 CPU1 )
    totals=( 12345 23456 )
    

    And output:

    for (( i = 0; i < core_count; i++ )); do
        echo "${names[$i]} -> ${totals[$i]}"
    done
    

    And the last approach:

    #!/bin/bash
    
    FILE="/proc/interrupts"
    
    output=$(awk 'NR==1 {
        core_count = NF
        for (i = 1; i <= core_count; i++)
            names[i-1] = $i
        next
    }
    /eth/ {
        for (i = 2; i <= 2+core_count; i++)
            totals[i-2] += $i
    }
    
    END {
        for (i = 0; i < core_count; i++)
            printf("%s=%d\n", names[i], totals[i])
    }
    ' ${FILE})
    
    core_count=$(echo "${output}" | wc -l)
    totals=( ${output} )
    

    Now the (regular) array looks like:

    totals=( CPU0=12345 CPU1=23456 )
    

    And you can parse it like:

    for x in "${totals[@]}"; do
        name=${x%=*}
        value=${x#*=}
        echo "${name} -> ${value}"
    done
    

    (note now that splitting CPU name and value occurs in the loop).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a php script to read a .txt file. The content of the
I need to read a value from the terminal in a bash script. I
I need to write a script in Matlab, which will read some data from
I need a script that inserts a number at some point into a file
I need to know how to read data from tabbed delimited text file in
I need a script to copy only the changed/modified and new files from my
I need a script to convert a comma seperated csv file to an excel
I need to write a PHP script that will print out results from a
I've written a Ruby script that is reading a file ( File.read() ) that
I am struggling to write a batch script which can read a CSV file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.