【问题标题】:SED invalid command code for JSON responseJSON 响应的 SED 无效命令代码
【发布时间】:2020-07-30 21:35:42
【问题描述】:

我正在尝试通过 sed 从无头 mac mini (catalina) 上的本地服务器 (https://regex101.com/r/qeGcGu/1) 中的 JSON 获取值。但是,我希望使用 sed 命令:

usr@mcMini  ~/Documents/qBitTorrent cat /tmp/json.out | sed -i.bak '"hash":"(.*?)"'
sed: 1: ""hash":"(.*?)"": invalid command code "
usr@mcMini  ~/Documents/qBitTorrent cat /tmp/json.out | sed -i.bak '\"hash\":\"(.*?)\"'
sed: 1: "\"hash\":\"(.*?)\"": unterminated regular expression
usr@mcMini  ~/Documents/qBitTorrent cat /tmp/json.out | sed -i '' '\"hash\":\"(.*?)\"'
sed: 1: "\"hash\":\"(.*?)\"": unterminated regular expression
usr@mcMini  ~/Documents/qBitTorrent cat /tmp/json.out | sed -i '' '"hash":"(.*?)"'
sed: 1: ""hash":"(.*?)"": invalid command code "

我试图从中获取字符串的文件是原始 json。

[{"added_on":1587102956,"amount_left":0,"auto_tmm":false,"availability":-1,"category":"radarr","completed":1218638934,"completion_on":1587108704,"dl_limit":-1,"dlspeed":0,"downloaded":1220894674,"downloaded_session":0,"eta":8640000,"f_l_piece_prio":false,"force_start":true,"hash":"87802183fc647548ec6efe18feb16149522f6aa0","last_activity":1587119220,"magnet_uri":"magnet:?xt=urn:btih:87802183fc647548ec6efe18feb16149522f6aa0&dn=Fantasia%202000%20(1999)%20%5b1080p%5d%20%5bYTS.AG%5d&tr=udp%3a%2f%2ftracker.coppersurfer.tk%3a6969%2fannounce&tr=udp%3a%2f%2f9.rarbg.com%3a2710%2fannounce&tr=udp%3a%2f%2fp4p.arenabg.com%3a1337&tr=udp%3a%2f%2ftracker.leechers-paradise.org%3a6969&tr=udp%3a%2f%2ftracker.internetwarriors.net%3a1337&tr=udp%3a%2f%2ftracker.opentrackr.org%3a1337%2fannounce&tr=udp%3a%2f%2ftracker.zer0day.to%3a1337%2fannounce&tr=udp%3a%2f%2ftracker.leechers-paradise.org%3a6969%2fannounce&tr=udp%3a%2f%2fcoppersurfer.tk%3a6969%2fannounce","max_ratio":-1,"max_seeding_time":-1,"name":"Fantasia 2000 (1999) [1080p] [YTS.AG]","num_complete":22,"num_incomplete":4,"num_leechs":0,"num_seeds":0,"priority":0,"progress":1,"ratio":0.1782183661159947,"ratio_limit":-2,"save_path":"/Volumes/1049/Media/","seeding_time_limit":-2,"seen_complete":1587118087,"seq_dl":false,"size":1218638934,"state":"forcedUP","super_seeding":false,"tags":"","time_active":13224,"total_size":1218638934,"tracker":"udp://tracker.coppersurfer.tk:6969/announce","up_limit":-1,"uploaded":217585854,"uploaded_session":128831791,"upspeed":0}]

实际上我想要完成的是从哈希中获取前 6 个字符: “哈希”:“87802183fc647548ec6efe18feb16149522f6aa0”

在这种情况下,我想要的值是 878021

你能指导我正确的方向吗?

【问题讨论】:

  • 对。你可以使用sed -n 's/.*"hash":"\([^"]*\).*/\1/p' /tmp/json.out
  • 收到this answer后怎么会出现这个问题?问题几乎相同。
  • 感谢您抽出宝贵的时间,它完成了这项工作。如您所见,我每年都在玩这个,所以我没有花时间正确分析。
  • 注意 sed 使用 POSIX 正则表达式语法,因此您不能在 sed 正则表达式中使用 *? / +? 非贪婪量词。另外,请记住分隔符,sed 在使用正则表达式时需要它们。

标签: regex macos sed terminal


【解决方案1】:

你可以使用

sed -n 's/.*"hash":"\([^"]*\).*/\1/p' /tmp/json.out

这里请注意,文件可以直接提供给sed 命令,无需使用cat 进行管道传输。

工作原理

  • -n - 禁止默认行输出的选项(默认情况下,sed 将输出不匹配的行)
  • s/ - 替换命令(我们正在替换)
  • .*"hash":"\([^"]*\).* - 匹配
    • .* - 0+ 个字符
    • "hash":" - "hash":" 子字符串
    • \([^"]*\) - 第 1 组(捕获组,替换部分使用 \1 来引用此值) - 除 " 之外的任何 0+ 字符
    • .* - 0+ 个字符
  • \1 - 替换为第 1 组值(匹配行上剩余的所有值)
  • p - 如果存在有效替换,则仅在替换后打印结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-18
    • 2021-12-25
    • 2019-04-08
    • 2014-02-23
    • 2013-10-15
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    相关资源
    最近更新 更多