jq
私はperを使用してJSONファイルをロードしようとしていますこここれは非常に簡単で、次のように機能します。
$ cat ~/Downloads/json.txt | jq '.name'
"web"
ただし、この変数の出力をコマンドに割り当てる必要があります。これを試してみたところ、次のように動作します。
$ my_json=`cat ~/Downloads/json.txt | jq '.name'`
$ myfile=~/Downloads/$my_json.txt
$ echo $myfile
/home/qut/Downloads/"web".txt
でも私はしたい/home/qut/Downloads/web.txt
。
引用符を削除して"web"
に変更するにはどうすればいいですかweb
?
答え1
あなたはtr引用符を削除するコマンド:
my_json=$(cat ~/Downloads/json.txt | jq '.name' | tr -d \")
答え2
の特定のケースではjq
、出力が生フォーマット:
--raw-output / -r:
With this option, if the filter´s result is a string then it will
be written directly to standard output rather than being formatted
as a JSON string with quotes. This can be useful for making jq fil‐
ters talk to non-JSON-based systems.
サンプルjson.txt
ファイルを使用して説明すると、あなたのリンク:
$ jq '.name' json.txt
"Google"
一方
$ jq -r '.name' json.txt
Google
答え3
ネイティブ シェルのプレフィックス/サフィックス削除機能を使用すると、よりシンプルで効率的です。
my_json=$(cat ~/Downloads/json.txt | jq '.name')
temp="${my_json%\"}"
temp="${temp#\"}"
echo "$temp"
ソースhttps://stackoverflow.com/questions/9733338/shell-script-remove-first-and-last-quote-from-a-variable
答え4
次のように使用できますeval echo
:
my_json=$(eval echo $(cat ~/Downloads/json.txt | jq '.name'))
しかし、これは理想的ではなく、簡単にバグやセキュリティ上の欠陥が発生する可能性があります。