如何基于AsicErr数值提取对应设备的FIA统计及命令行信息?
Got it, let's break down how to pull those targeted blocks with the highest AsicErr counts along with their required context. Your intuition about working backwards from the error values is spot-on—we'll use awk for block processing (perfect for this structured network device output) and a quick pre-step to capture the top error values we care about.
1. First, Capture the Top AsicErr Values
First, we'll extract the highest N AsicErr counts (adjust head -3 to match how many top entries you want) and store them for later lookup:
# Extract top 3 AsicErr numerical values (update head -N for more/fewer entries) TOP_ERRS=$(grep AsicErr file.txt | sort -t: -k2nr | head -3 | awk '{print $4}')
sort -t: -k2nrsorts the AsicErr lines by the numerical count (second field, colon-separated) in reverse order- We pull just the numerical value with
awk '{print $4}'to make matching easier later
2. Use Awk to Process the File in Blocks
Each command output group starts with a line ending in <<<<<<<<<<<<<<<<<<<<, so we'll use that as our block delimiter. The awk script will:
- Track each block as we parse the file
- Cache the 3 key lines you need: the cleaned show command line (without arrows), the FIA Statistics line, and the matching AsicErr line
- Output the cached lines only if the block contains one of our top error values
Save This as a Reusable Script
Create a file named extract_err_blocks.awk with this content:
BEGIN { # Split our top error values into a lookup array for fast checks split(ENVIRON["TOP_ERRS"], err_vals, " ") for (v in err_vals) err_set[err_vals[v]] = 1 } # Trigger: Start of a new command output block /<<<<<<<<<<<<<<<<<<<<$/ { # If we have a valid cached block from before, output it first if (show_line != "" && fia_line != "" && matched_err != "") { print show_line print fia_line print matched_err print "" # Add a blank line between blocks for readability } # Clean up the show command line (remove trailing arrows) show_line = substr($0, 1, length($0)-18) # 18 = length of "<<<<<<<<<<<<<<<<<<<<", adjust if needed # Reset cache for the new block fia_line = "" matched_err = "" next } # Capture the FIA Statistics header line /^FIA Statistics/ { fia_line = $0 next } # Check if this AsicErr line is in our top values set /^EGQ0 AsicErr :/ { err_val = $4 if (err_val in err_set) { matched_err = $0 } next } # End of file: Output the last valid block if it exists END { if (show_line != "" && fia_line != "" && matched_err != "") { print show_line print fia_line print matched_err } }
Run the Script
Execute it with the TOP_ERRS variable we created earlier:
TOP_ERRS="$TOP_ERRS" awk -f extract_err_blocks.awk file.txt
3. Alternative: One-Liner Version
If you prefer not to save a separate script, you can embed the logic directly into a single command:
TOP_ERRS=$(grep AsicErr file.txt | sort -t: -k2nr | head -3 | awk '{print $4}') && \ awk -v errs="$TOP_ERRS" ' BEGIN {split(errs, e, " "); for(i in e) err[e[i]]=1} /<<<<<<<<<<<<<<<<<<<<$/ {if(s&&f&&m){print s"\n"f"\n"m"\n"} s=substr($0,1,length($0)-18); f=""; m=""; next} /^FIA Statistics/ {f=$0; next} /^EGQ0 AsicErr :/ {if($4 in err) m=$0; next} END {if(s&&f&&m) print s"\n"f"\n"m} ' file.txt
Key Notes
- The
substrcall removes the trailing<<<<<<<<<<<<<<<<<<<<from the show command line to match your desired output format - We only cache and output the exact lines you need, avoiding extra clutter from the full command output
- The lookup array (
err_set) ensures we quickly match only the top AsicErr values we care about
内容的提问来源于stack exchange,提问作者user

