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

使用awk替换匹配模式为特殊字符并提取HTTP响应的nonce值

Hey there! Let's work through your two questions step by step—they're both common text processing tasks, so I've got you covered.

1. Using awk to Replace a Matched Pattern with a Special Character

awk has two go-to functions for replacements, depending on whether you want to change the first match per line or all matches:

  • Replace first occurrence per line: Use sub(). The syntax is straightforward:
    awk '{sub(/your_target_pattern/, "your_special_character"); print}' your_input_file
    
    Example: Swap the first "warning" in each line with a warning emoji:
    awk '{sub(/warning/, "⚠️"); print}' app.log
    
  • Replace all occurrences globally: Use gsub() (the "g" stands for global). This changes every match in every line:
    awk '{gsub(/error/, "❌"); print}' app.log
    
    Pro tip: If your pattern includes regex special characters like / or ., escape them with a backslash (e.g., \/ for a literal slash) or use string matching instead (wrap the pattern in quotes, like gsub("http://example.com", "🔗")). Most UTF-8 special characters work directly as replacement values, as long as your terminal supports them.
2. Extracting the nonce Value from a 401 HTTP Response

First, let's recap the HTTP response you're working with:

HTTP/1.1 401 Unauthorized
Server: WinREST HTTP Server/1.0
Connection: Keep-Alive
Content-Type: text/html
Content-Length: 89
WWW-Authenticate: ServiceAuth realm="WinREST", nonce="1828HvF7EfPnRtzSs/h10Q=="

UnauthorizedError 401: Unauthorized

Your initial grep -oP 'nonce="\...' didn't work because \... only matches three arbitrary characters—we need to grab everything up to the closing double quote. Here are three reliable solutions:

Option 1: Fixed grep (PCRE Mode)

Use \K to "reset" the match, so we only output the part we care about:

grep -oP 'nonce="\K[^"]+' your_http_file.txt

Breakdown:

  • -o: Only output the matched portion of the line
  • -P: Enable Perl-compatible regular expressions (PCRE)
  • nonce="\K: Match nonce=" but discard it from the output
  • [^"]+: Match every character that isn't a double quote (stopping at the closing " around the nonce)

Option 2: awk (Field Splitting)

Split the line using custom delimiters to directly pull the nonce value:

awk -F'nonce="|"' '/nonce=/ {print $2}' your_http_file.txt

Breakdown:

  • -F'nonce="|"': Set the field separator to either nonce=" or a double quote
  • /nonce=/: Only process lines that contain the nonce= string
  • print $2: The second field will be exactly the nonce value between the quotes

Option 3: sed (Capture Group)

Use sed's substitution to capture the nonce and print only that:

sed -n 's/.*nonce="\([^"]\+\)".*/\1/p' your_http_file.txt

Breakdown:

  • -n: Don't print lines by default
  • s/.*nonce="\([^"]\+\)".*/\1/p: Match the entire line, capture the text between nonce=" and ", replace the whole line with that captured text, and print it

All three commands will output exactly what you need: 1828HvF7EfPnRtzSs/h10Q==

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 12:30:39