使用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.
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:
Example: Swap the first "warning" in each line with a warning emoji:awk '{sub(/your_target_pattern/, "your_special_character"); print}' your_input_fileawk '{sub(/warning/, "⚠️"); print}' app.log - Replace all occurrences globally: Use
gsub()(the "g" stands for global). This changes every match in every line:
Pro tip: If your pattern includes regex special characters likeawk '{gsub(/error/, "❌"); print}' app.log/or., escape them with a backslash (e.g.,\/for a literal slash) or use string matching instead (wrap the pattern in quotes, likegsub("http://example.com", "🔗")). Most UTF-8 special characters work directly as replacement values, as long as your terminal supports them.
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=="Unauthorized Error 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: Matchnonce="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 eithernonce="or a double quote/nonce=/: Only process lines that contain thenonce=stringprint $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 defaults/.*nonce="\([^"]\+\)".*/\1/p: Match the entire line, capture the text betweennonce="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

