0

JSON を解析するスクリプトを使用しています。私は知っています、私は知っています、あなたはそれをするべきではありません。しかし、スクリプトはそれを大いに利用しています。そして、それが問題に遭遇する理由です。私は BusyBox で使用していますが、最小限のバージョンの sed しかありません。

ここで問題が発生します。これは JSON 出力です。

{"expires": "Thu, 11 Oct 2012 11:30:29 +0000", "upload_id": "hhgJHflih753jDhhod", "offset": 293876}

これは、busybox で動作する「オフセット」の値を取得するために使用される sed コマンドです。

sed -n -e 's/.*"offset":\s*\([^}]*\).*/\1/p'

これは、BusyBox では機能しない upload_id を取得するコマンドです。

sed -n -e 's/.*"upload_id":\s*"*\([^"]*\)"*.*/\1/p'

ミニマルなsedを使用してBusyBoxで「upload_id」を取得するのを手伝ってくれる人がいれば、本当に感謝しています。ありがとう!

4

3 に答える 3

0

One way using awk:

awk -F "[:,]" '{ for (i=1; i<=NF; i++) if ($i ~ /upload_id/) { gsub(/[" {}]/,"",$(i+1)); print $(i+1) } }' file.txt

Result:

hhgJHflih753jDhhod

Note that this will work for the offset and upload_id fields, but not the expires field. HTH.

于 2012-10-10T12:22:11.893 に答える
0

使用cut:

$ # get upload_id
$ echo '{"expires": "Thu, 11 Oct 2012 11:30:29 +0000", "upload_id": "hhgJHflih753jDhhod", "offset": 293876}' |\
cut -d'"' -f8
hhgJHflih753jDhhod
$ # get offset
$ echo '{"expires": "Thu, 11 Oct 2012 11:30:29 +0000", "upload_id": "hhgJHflih753jDhhod", "offset": 293876}' |\
cut -d':' -f6 | cut -d' ' -f2 | cut -d'}' -f1
293876

これは可能な限りミニマルです。

編集:

使用gawk:

$ echo '{"expires": "Thu, 11 Oct 2012 11:30:29 +0000", "upload_id": "hhgJHflih753jDhhod", "offset": 293876}' |\
gawk -F'"' '{print $8}'       
hhgJHflih753jDhhod
于 2012-10-10T12:15:17.570 に答える
0

grep:

grep -Po '(?<=upload_id": ")[^"]*' 

シード:

sed -r 's/.*upload_id": "([^"]*).*/\1/'
于 2012-10-10T12:37:14.653 に答える