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

无Perl支持,如何用awk/sed/grep提取JSON中authToken值(无需正向预查)

在OpenWrt Bash脚本中提取JSON里的AuthToken值(无需正向预查)

你提到用PCRE的正向预查正则能提取目标token,但OpenWrt环境没Perl支持,这几个用sed、awk、grep的方法绝对能用——都是OpenWrt默认自带的基础工具,不用额外装软件:

方法1:使用sed(最简洁)

直接通过字符串替换捕获目标内容,一行搞定:

echo '{"user":{"id":1325135,"uuid":"134513451","email":"ansdfaha@aol.com","joined_at":"2012-01-01T013:511:124.000Z","username":"testicl","title":"testli","thumb":"https://plex.tv/user/avatar?c=","hasPassword":true,"authToken":"RJ3fadfiasdUYBxF6z","authentication_token":"RJ3fadfiasdUYBxF6z","subscription":{"active":false,"status":"Inactive","plan":null,"features":["adaptive_bitrate","collections","photos-metadata-edition","radio","photos-favorites","federated-auth","Android - PiP","publishing_platform","news","kevin-bacon","client-radio-stations","TREBLE-show-features","web_server_dashboard","conan_redirect_qa","conan_redirect_alpha","conan_redirect_beta","transcoder_cache"]},"roles":{"roles":[]},"entitlements":[],"confirmedAt":"2012-01-01T13:31:31.000Z","forumId":23573,"rememberMe":false}}' | sed 's/.*"authToken":"\([^"]*\)".*/\1/'

逻辑说明:

  • .*"authToken":" 匹配从字符串开头到"authToken":"的所有内容
  • \([^"]*\) 捕获所有非双引号的字符(也就是我们要的token),用括号标记为捕获组
  • .* 匹配捕获组之后的所有内容
  • 最后把整个字符串替换为第一个捕获组的内容(\1)

方法2:使用awk(分步分割)

如果更习惯awk的文本分割逻辑,可以分两次切割字符串:

echo '{"user":{"id":1325135,"uuid":"134513451","email":"ansdfaha@aol.com","joined_at":"2012-01-01T013:511:124.000Z","username":"testicl","title":"testli","thumb":"https://plex.tv/user/avatar?c=","hasPassword":true,"authToken":"RJ3fadfiasdUYBxF6z","authentication_token":"RJ3fadfiasdUYBxF6z","subscription":{"active":false,"status":"Inactive","plan":null,"features":["adaptive_bitrate","collections","photos-metadata-edition","radio","photos-favorites","federated-auth","Android - PiP","publishing_platform","news","kevin-bacon","client-radio-stations","TREBLE-show-features","web_server_dashboard","conan_redirect_qa","conan_redirect_alpha","conan_redirect_beta","transcoder_cache"]},"roles":{"roles":[]},"entitlements":[],"confirmedAt":"2012-01-01T13:31:31.000Z","forumId":23573,"rememberMe":false}}' | awk -F'"authToken":"' '{print $2}' | awk -F'"' '{print $1}'

逻辑说明:

  1. 第一次用"authToken":"作为分隔符,把字符串切成两部分,取第二部分(也就是token开头到结尾的内容)
  2. 第二次用双引号作为分隔符,取第一部分(就是纯token值)

方法3:使用grep(两次提取)

如果OpenWrt的grep支持-o参数(默认应该支持),可以用两次grep精准提取:

echo '{"user":{"id":1325135,"uuid":"134513451","email":"ansdfaha@aol.com","joined_at":"2012-01-01T013:511:124.000Z","username":"testicl","title":"testli","thumb":"https://plex.tv/user/avatar?c=","hasPassword":true,"authToken":"RJ3fadfiasdUYBxF6z","authentication_token":"RJ3fadfiasdUYBxF6z","subscription":{"active":false,"status":"Inactive","plan":null,"features":["adaptive_bitrate","collections","photos-metadata-edition","radio","photos-favorites","federated-auth","Android - PiP","publishing_platform","news","kevin-bacon","client-radio-stations","TREBLE-show-features","web_server_dashboard","conan_redirect_qa","conan_redirect_alpha","conan_redirect_beta","transcoder_cache"]},"roles":{"roles":[]},"entitlements":[],"confirmedAt":"2012-01-01T13:31:31.000Z","forumId":23573,"rememberMe":false}}' | grep -o '"authToken":"[^"]*"' | grep -o '[^"]*$'

逻辑说明:

  1. 第一次grep -o只输出匹配"authToken":"[^"]*"的部分,也就是"authToken":"RJ3fadfiasdUYBxF6z"
  2. 第二次grep -o '[^"]*$'提取这部分内容里最后一个双引号之前的所有字符,也就是token值

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 06:29:35