在 JSON 文件中就地更改文本(使用任何工具)

在 JSON 文件中就地更改文本(使用任何工具)

我在具有端口转发的 VPN 上,并且已经有了脚本来获取获得的转发端口,在 中打开该端口iptables,但我没有在启动传输设置文件之前自动更改该端口。

我的传输设置文件:/home/vlastimil/.config/transmission/settings.json包含:

{
    "alt-speed-down": 50,
    "alt-speed-enabled": false,
    "alt-speed-time-begin": 540,
    "alt-speed-time-day": 127,
    "alt-speed-time-enabled": false,
    "alt-speed-time-end": 1020,
    "alt-speed-up": 0,
    "bind-address-ipv4": "0.0.0.0",
    "bind-address-ipv6": "::",
    "blocklist-date": 1591362636,
    "blocklist-enabled": false,
    "blocklist-updates-enabled": true,
    "blocklist-url": "http://list.iblocklist.com/?list=ydxerpxkpcfqjaybcssw&fileformat=p2p&archiveformat=gz",
    "cache-size-mb": 4096,
    "compact-view": false,
    "details-window-height": 1010,
    "details-window-width": 1562,
    "dht-enabled": true,
    "download-dir": "/home/vlastimil/Downloads",
    "download-queue-enabled": false,
    "download-queue-size": 1,
    "encryption": 0,
    "idle-seeding-limit": 30,
    "idle-seeding-limit-enabled": false,
    "incomplete-dir": "/home/vlastimil/Downloads",
    "incomplete-dir-enabled": false,
    "inhibit-desktop-hibernation": true,
    "lpd-enabled": false,
    "main-window-height": 442,
    "main-window-is-maximized": 0,
    "main-window-width": 664,
    "main-window-x": 1256,
    "main-window-y": 570,
    "message-level": 2,
    "open-dialog-dir": "/home/vlastimil/Downloads",
    "peer-congestion-algorithm": "",
    "peer-id-ttl-hours": 6,
    "peer-limit-global": 200,
    "peer-limit-per-torrent": 50,
    "peer-port": 24833,
    "peer-port-random-high": 65535,
    "peer-port-random-low": 49152,
    "peer-port-random-on-start": false,
    "peer-socket-tos": "default",
    "pex-enabled": true,
    "port-forwarding-enabled": false,
    "preallocation": 2,
    "prefetch-enabled": true,
    "queue-stalled-enabled": true,
    "queue-stalled-minutes": 30,
    "ratio-limit": 2,
    "ratio-limit-enabled": false,
    "recent-download-dir-1": "/home/vlastimil/Downloads",
    "recent-download-dir-2": "/home/vlastimil/Downloads/memtest/PRO/torrent",
    "recent-download-dir-3": "/home/vlastimil/Downloads/memtest/Pro",
    "recent-download-dir-4": "/media/vlastimil/4TB_Seagate_NTFS/Movies",
    "rename-partial-files": true,
    "rpc-authentication-required": false,
    "rpc-bind-address": "0.0.0.0",
    "rpc-enabled": true,
    "rpc-host-whitelist": "",
    "rpc-host-whitelist-enabled": true,
    "rpc-password": "{426d7fbcb4015f3821d212e0203d5e20033661141zKMndND",
    "rpc-port": 9091,
    "rpc-url": "/transmission/",
    "rpc-username": "",
    "rpc-whitelist": "127.0.0.1",
    "rpc-whitelist-enabled": true,
    "scrape-paused-torrents-enabled": true,
    "script-torrent-done-enabled": false,
    "script-torrent-done-filename": "/home/vlastimil",
    "seed-queue-enabled": false,
    "seed-queue-size": 10,
    "show-backup-trackers": true,
    "show-extra-peer-details": false,
    "show-filterbar": false,
    "show-notification-area-icon": true,
    "show-options-window": true,
    "show-statusbar": true,
    "show-toolbar": true,
    "show-tracker-scrapes": true,
    "sort-mode": "sort-by-age",
    "sort-reversed": false,
    "speed-limit-down": 2048,
    "speed-limit-down-enabled": false,
    "speed-limit-up": 0,
    "speed-limit-up-enabled": false,
    "start-added-torrents": false,
    "statusbar-stats": "total-transfer",
    "torrent-added-notification-enabled": false,
    "torrent-complete-notification-enabled": false,
    "torrent-complete-sound-command": "canberra-gtk-play -i complete-download -d 'transmission torrent downloaded'",
    "torrent-complete-sound-enabled": false,
    "trash-can-enabled": true,
    "trash-original-torrent-files": false,
    "umask": 18,
    "upload-slots-per-torrent": 14,
    "user-has-given-informed-consent": true,
    "utp-enabled": true,
    "watch-dir": "/home/vlastimil/Downloads",
    "watch-dir-enabled": false
}

它位于第 41 行,但严格来说我不确定它是否已修复,但为了这个问题而将其修复。


所以,我有一个新的端口号,比如 65535,并且想要一个到位更改旧端口号。

我不知道如何使用sedor awk,虽然我觉得这会有点极其简单的。谢谢。

答案1

用于jq将顶级peer-port键的值更新为 65535:

file=/home/vlastimil/.config/transmission/settings.json

cp "$file" "$file.tmp" &&
jq '."peer-port" |= 65535' "$file.tmp" >"$file" &&
rm -f "$file.tmp"

使用带有新端口值的 shell 变量:

newport=65535
file=/home/vlastimil/.config/transmission/settings.json

cp "$file" "$file.tmp" &&
jq --argjson new "$newport" '."peer-port" |= $new' "$file.tmp" >"$file" &&
rm -f "$file.tmp"

由于jq无法自行进行就地编辑,因此我在文件的临时副本上运行它,同时将结果重定向到原始名称。这样做(而不是jq在原始文件上运行,重定向到新的临时文件,然后使用mv将新文件移动到旧文件上)可以保留原始文件的元数据(例如所有权和权限)。

如果出现问题,例如,如果 shell 变量的值$newport不是数字,则临时文件将不会被删除,并且它将保存未修改的文档。

答案2

使用sed

sed -i 's/"peer-port": 12345/"peer-port": 65535/' settings.json

相关内容