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'))
그러나 이는 이상적인 것은 아닙니다. 버그 및/또는 보안 결함이 쉽게 발생할 수 있습니다.