You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWK能否比Bash循环更快地转置指定格式的数据?

AWK Solution for Reshaping Scan Data

Here's an efficient AWK script that processes your data in a single pass, eliminating the need for multiple grep/cut calls in loops:

BEGIN {
    FS = ","
    # Define the desired column order for output
    cols[1] = "LAST_VULN_AGENT_SCAN"
    cols[2] = "LAST_VULN_NONCRED_SCAN"
    cols[3] = "LAST_VULN_CRED_SCAN"
}

{
    target = $1
    scan_type = $2
    timestamp = $3
    # Store timestamp mapped to target and scan type
    data[target][scan_type] = timestamp
    # Track unique targets to maintain order
    if (!(target in targets)) {
        targets[target] = 1
        target_list[++num_targets] = target
    }
}

END {
    # Print header row
    printf "Name"
    for (i = 1; i <= 3; i++) {
        printf ",%s", cols[i]
    }
    printf "\n"
    
    # Print each target's consolidated row
    for (i = 1; i <= num_targets; i++) {
        current_target = target_list[i]
        printf "%s", current_target
        for (j = 1; j <= 3; j++) {
            printf ",%s", data[current_target][cols[j]]
        }
        printf "\n"
    }
}

How to Use

Run it with your existing DATA variable like this:

echo "$DATA" | awk -f script.awk

Or use the inline version if you prefer:

echo "$DATA" | awk 'BEGIN {FS=","; cols[1]="LAST_VULN_AGENT_SCAN"; cols[2]="LAST_VULN_NONCRED_SCAN"; cols[3]="LAST_VULN_CRED_SCAN"} {target=$1; scan_type=$2; timestamp=$3; data[target][scan_type]=timestamp; if(!(target in targets)) {targets[target]=1; target_list[++num_targets]=target}} END {printf "Name"; for(i=1;i<=3;i++) printf ",%s", cols[i]; print ""; for(i=1;i<=num_targets;i++) {current_target=target_list[i]; printf "%s", current_target; for(j=1;j<=3;j++) printf ",%s", data[current_target][cols[j]]; print ""}}'

Key Advantages Over Bash Loop

  • Single Pass Processing: The AWK script reads your data once instead of reprocessing it for every target and scan type, drastically improving performance with large datasets.
  • Cleaner Logic: Uses associative arrays to map targets to their scan timestamps, avoiding messy nested grep/cut commands.
  • Maintains Order: Preserves the order of targets as they first appear in the input (you can sort target_list in the END block if you need alphabetical order).

内容的提问来源于stack exchange,提问作者27560

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 06:45:32