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

求助:用AWK脚本过滤日志并提取指定参数到Shell变量

Hey there! Let's break this down step by step since you're new to shell scripting and awk—no worries, we'll get you sorted. First, let's start with a realistic example of what your log might look like (since you didn't share the exact format, I'll assume space-separated key-value pairs, which is common):

processor=gateway1 paymentType=credit amount=100.50 currency=USD network=VISA responseCode=00
processor=gateway2 paymentType=debit amount=75.00 currency=EUR network=MASTER responseCode=00
processor=gateway1 paymentType=credit amount=200.00 currency=GBP network=AMEX responseCode=01
processor=gateway3 paymentType=credit amount=50.00 currency=USD network=VISA responseCode=00

Step 1: Filter rows by processor and paymentType

Your initial command only pulled lines with "amount", but we need to first narrow down to rows that match your target processor and payment type. Let's say you want processor=gateway1 and paymentType=credit—here's how to filter those with awk:

awk '$1 == "processor=gateway1" && $2 == "paymentType=credit"' testAPI.log

If the order of fields isn't fixed (e.g., processor might not always be first), a more robust approach checks every field for the keys:

awk '{
    match_proc=0; match_pay=0
    for (i=1; i<=NF; i++) {
        split($i, kv, "=")
        if (kv[1] == "processor" && kv[2] == "gateway1") match_proc=1
        if (kv[1] == "paymentType" && kv[2] == "credit") match_pay=1
    }
    if (match_proc && match_pay) print $0
}' testAPI.log

Step 2: Extract the required fields from filtered rows

Now that we have the right rows, we need to pull out amount, currency, network, and responseCode. We'll extend the awk script to capture these values and print them in a clean format:

awk '{
    match_proc=0; match_pay=0
    amt=""; curr=""; net=""; rc=""
    # First check if the row matches our filters
    for (i=1; i<=NF; i++) {
        split($i, kv, "=")
        if (kv[1] == "processor" && kv[2] == "gateway1") match_proc=1
        if (kv[1] == "paymentType" && kv[2] == "credit") match_pay=1
        # Capture the values we need while we're looping
        if (kv[1] == "amount") amt=kv[2]
        if (kv[1] == "currency") curr=kv[2]
        if (kv[1] == "network") net=kv[2]
        if (kv[1] == "responseCode") rc=kv[2]
    }
    # Only print if the row matched our filters
    if (match_proc && match_pay) print amt, curr, net, rc
}' testAPI.log

Step 3: Store extracted values in shell variables

To use these values in your Oracle insert command, we can read awk's output into shell variables. If you only expect one matching row, use read:

read amount currency network responseCode <<< $(awk '{
    match_proc=0; match_pay=0
    amt=""; curr=""; net=""; rc=""
    for (i=1; i<=NF; i++) {
        split($i, kv, "=")
        if (kv[1] == "processor" && kv[2] == "gateway1") match_proc=1
        if (kv[1] == "paymentType" && kv[2] == "credit") match_pay=1
        if (kv[1] == "amount") amt=kv[2]
        if (kv[1] == "currency") curr=kv[2]
        if (kv[1] == "network") net=kv[2]
        if (kv[1] == "responseCode") rc=kv[2]
    }
    if (match_proc && match_pay) print amt, curr, net, rc
}' testAPI.log)

# Verify the variables work
echo "Amount: $amount"
echo "Currency: $currency"
echo "Network: $network"
echo "Response Code: $responseCode"

If there are multiple matching rows, loop through each one and insert them into Oracle:

awk '{
    match_proc=0; match_pay=0
    amt=""; curr=""; net=""; rc=""
    for (i=1; i<=NF; i++) {
        split($i, kv, "=")
        if (kv[1] == "processor" && kv[2] == "gateway1") match_proc=1
        if (kv[1] == "paymentType" && kv[2] == "credit") match_pay=1
        if (kv[1] == "amount") amt=kv[2]
        if (kv[1] == "currency") curr=kv[2]
        if (kv[1] == "network") net=kv[2]
        if (kv[1] == "responseCode") rc=kv[2]
    }
    if (match_proc && match_pay) print amt, curr, net, rc
}' testAPI.log | while read amount currency network responseCode; do
    # Replace this with your actual Oracle insert command
    sqlplus your_username/your_password@your_database << EOF
    INSERT INTO your_table (amount, currency, network, response_code) 
    VALUES ($amount, '$currency', '$network', '$responseCode');
    COMMIT;
EOF
done

Quick notes for beginners:

  • Replace gateway1, credit, and the Oracle details with your actual values
  • For string fields like currency, wrap them in single quotes in the SQL insert (since they're not numeric)
  • If your log uses a different separator (not spaces), adjust the awk logic (e.g., change NF handling or split delimiter)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:35:02