无法将sed输出存储到变量

时间:2022-07-05 06:28:29

I am new to bash script. I am getting some json response and i get only one property from the response. I want to save it to a variable but it is not working token=$result |sed -n -e 's/^.*access_token":"//p' | cut -d'"' -f1 echo $token it returns blank line. I cannot use jq or any third party tools. Please let me know what I am missing.

我是bash脚本的新手。我得到一些json响应,我从响应中只得到一个属性。我想将它保存到变量但是它不能正常工作令牌= $ result | sed -n -e's /^.* access_token“:”// p'| cut -d'“' - f1 echo $ token它返回空白行。我不能使用jq或任何第三方工具。请让我知道我错过了什么。

2 个解决方案

#1


0  

Give this a try:

尝试一下:

token="$(sed -E -n -e 's/^.*access_token": ?"//p' <<<"$result" | cut -d'"' -f1)"

Explanation:

  • token="$( script here )" means that $token is set to the output/result of the script run inside the subshell through a process known as command substituion
  • token =“$(script here)”表示$ token被设置为通过称为命令替换的进程在子shell内运行的脚本的输出/结果

  • -E in sed allows Extended Regular Expressions. We want this because JSON generally contains a space after the : and before the next ". We use the ? after the space to tell sed that the space may or may not be present.
  • -E in sed允许扩展正则表达式。我们想要这个,因为JSON通常在:和下一个之后包含一个空格。我们在空格之后使用?告诉sed该空间可能存在也可能不存在。

  • <<<"$result" is a herestring that feeds the data into sed as stdin in place of a file.
  • <<<“$ result”是一个herestring,它将数据作为stdin提供给sed代替文件。

#2


0  

Your command should be:

你的命令应该是:

token=$(echo "$result" | sed -n -e 's/^.*access_token":"//p' | cut -d'"' -f1)

You need to use echo to print the contents of the variable over standard output, and you need to use a command substitution $( ) to assign the output of the pipeline to token.

您需要使用echo在标准输出上打印变量的内容,并且需要使用命令替换$()将管道的输出分配给令牌。

Quoting your variables is always encouraged, to avoid problems with white space and glob characters like *.

始终鼓励引用变量,以避免出现空格和像*这样的全局字符问题。


As an aside, note that you can probably obtain the output using something like:

顺便说一句,请注意您可以使用以下内容获取输出:

token=$(jq -r .access_token <<<"$result")

I know you've said that you can't use jq but it's a standalone binary (no need to install it) and treats your JSON in the correct way, not as arbitrary text.

我知道你已经说过你不能使用jq但它是一个独立的二进制文件(不需要安装它)并以正确的方式处理你的JSON,而不是任意文本。

#1


0  

Give this a try:

尝试一下:

token="$(sed -E -n -e 's/^.*access_token": ?"//p' <<<"$result" | cut -d'"' -f1)"

Explanation:

  • token="$( script here )" means that $token is set to the output/result of the script run inside the subshell through a process known as command substituion
  • token =“$(script here)”表示$ token被设置为通过称为命令替换的进程在子shell内运行的脚本的输出/结果

  • -E in sed allows Extended Regular Expressions. We want this because JSON generally contains a space after the : and before the next ". We use the ? after the space to tell sed that the space may or may not be present.
  • -E in sed允许扩展正则表达式。我们想要这个,因为JSON通常在:和下一个之后包含一个空格。我们在空格之后使用?告诉sed该空间可能存在也可能不存在。

  • <<<"$result" is a herestring that feeds the data into sed as stdin in place of a file.
  • <<<“$ result”是一个herestring,它将数据作为stdin提供给sed代替文件。

#2


0  

Your command should be:

你的命令应该是:

token=$(echo "$result" | sed -n -e 's/^.*access_token":"//p' | cut -d'"' -f1)

You need to use echo to print the contents of the variable over standard output, and you need to use a command substitution $( ) to assign the output of the pipeline to token.

您需要使用echo在标准输出上打印变量的内容,并且需要使用命令替换$()将管道的输出分配给令牌。

Quoting your variables is always encouraged, to avoid problems with white space and glob characters like *.

始终鼓励引用变量,以避免出现空格和像*这样的全局字符问题。


As an aside, note that you can probably obtain the output using something like:

顺便说一句,请注意您可以使用以下内容获取输出:

token=$(jq -r .access_token <<<"$result")

I know you've said that you can't use jq but it's a standalone binary (no need to install it) and treats your JSON in the correct way, not as arbitrary text.

我知道你已经说过你不能使用jq但它是一个独立的二进制文件(不需要安装它)并以正确的方式处理你的JSON,而不是任意文本。