如何在shell中将包含JSON的多行变量转换为单行?

ufj5ltwl  于 2022-11-16  发布在  Shell
关注(0)|答案(3)|浏览(483)

我在shell中有以下变量

{
  "type": "service_account",
  "project_id": "projectid234",
  "private_key_id": "aasdadsxzce5",
  "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCiWjA5+xF6gsGC\nOknIL9DOCQpV2LRXYPEKl5+mXuN45vwqh6QvG4lw/Hi7EJhtAn+FQy7+yOQYrw3l\nQ2CpxDotT+PT2OuQ6LVbc/F+SblPlrK3B+8aEMo57PZ+gnwMcQ7+ofPnzC635uUP\npOG0idMTK\n-----END PRIVATE KEY-----\n",
  "client_email": "clinetmail@asdasd3435.iam.gserviceaccount.com",
  "client_id": "1234242342341",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/assadjashd.iam.gserviceaccount.com"
}

如下图:

export var1='{
      "type": "service_account",
      "project_id": "projectid234",
      "private_key_id": "aasdadsxzce5",
      "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCiWjA5+xF6gsGC\nOknIL9DOCQpV2LRXYPEKl5+mXuN45vwqh6QvG4lw/Hi7EJhtAn+FQy7+yOQYrw3l\nQ2CpxDotT+PT2OuQ6LVbc/F+SblPlrK3B+8aEMo57PZ+gnwMcQ7+ofPnzC635uUP\npOG0idMTK\n-----END PRIVATE KEY-----\n",
      "client_email": "clinetmail@asdasd3435.iam.gserviceaccount.com",
      "client_id": "1234242342341",
      "auth_uri": "https://accounts.google.com/o/oauth2/auth",
      "token_uri": "https://oauth2.googleapis.com/token",
      "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
      "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/assadjashd.iam.gserviceaccount.com"
    }'

现在我想把它转换成一个单行,我该怎么办?我尝试了各种解决方案,但他们没有工作。例如,我用下面的代码,我发现,但转换成base64和解码后,它只是返回第一行。你介意帮助我吗?

$((echo $var1 | tr -d '\n') | base64)
mbzjlibv

mbzjlibv1#

由于您要处理JSON,我建议使用jq之类的工具,它提供了--compact-output选项,并且能够使用@base64编码为Base64
至单行输出

jq --compact-output '' <<< "$test"

至Base64单行输出

jq --compact-output  '@base64' <<< "$test"

以Base64单行输出,并用bash解码

jq --compact-output --raw-output '@base64' <<< "$test" | base64 --decode

注意要删除""--raw-output

上述行的局部shell示例:

$ test='{
  "type": "service_account",
  "project_id": "projectid234",
  "private_key_id": "aasdadsxzce5",
  "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCiWjA5+xF6gsGC\nOknIL9DOCQpV2LRXYPEKl5+mXuN45vwqh6QvG4lw/Hi7EJhtAn+FQy7+yOQYrw3l\nQ2CpxDotT+PT2OuQ6LVbc/F+SblPlrK3B+8aEMo57PZ+gnwMcQ7+ofPnzC635uUP\npOG0idMTK\n-----END PRIVATE KEY-----\n",
  "client_email": "clinetmail@asdasd3435.iam.gserviceaccount.com",
  "client_id": "1234242342341",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/assadjashd.iam.gserviceaccount.com"
}'
$
$ jq --compact-output '' <<< "$test"
{"type":"service_account","project_id":"projectid234","private_key_id":"aasdadsxzce5","private_key":"-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCiWjA5+xF6gsGC\nOknIL9DOCQpV2LRXYPEKl5+mXuN45vwqh6QvG4lw/Hi7EJhtAn+FQy7+yOQYrw3l\nQ2CpxDotT+PT2OuQ6LVbc/F+SblPlrK3B+8aEMo57PZ+gnwMcQ7+ofPnzC635uUP\npOG0idMTK\n-----END PRIVATE KEY-----\n","client_email":"clinetmail@asdasd3435.iam.gserviceaccount.com","client_id":"1234242342341","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_x509_cert_url":"https://www.googleapis.com/robot/v1/metadata/x509/assadjashd.iam.gserviceaccount.com"}
$
$
$ jq --compact-output '@base64' <<< "$test"
"eyJ0eXBlIjoic2VydmljZV9hY2NvdW50IiwicHJvamVjdF9pZCI6InByb2plY3RpZDIzNCIsInByaXZhdGVfa2V5X2lkIjoiYWFzZGFkc3h6Y2U1IiwicHJpdmF0ZV9rZXkiOiItLS0tLUJFR0lOIFBSSVZBVEUgS0VZLS0tLS1cbk1JSUV2Z0lCQURBTkJna3Foa2lHOXcwQkFRRUZBQVNDQktnd2dnU2tBZ0VBQW9JQkFRQ2lXakE1K3hGNmdzR0Ncbk9rbklMOURPQ1FwVjJMUlhZUEVLbDUrbVh1TjQ1dndxaDZRdkc0bHcvSGk3RUpodEFuK0ZReTcreU9RWXJ3M2xcblEyQ3B4RG90VCtQVDJPdVE2TFZiYy9GK1NibFBsckszQis4YUVNbzU3UForZ253TWNRNytvZlBuekM2MzV1VVBcbnBPRzBpZE1US1xuLS0tLS1FTkQgUFJJVkFURSBLRVktLS0tLVxuIiwiY2xpZW50X2VtYWlsIjoiY2xpbmV0bWFpbEBhc2Rhc2QzNDM1LmlhbS5nc2VydmljZWFjY291bnQuY29tIiwiY2xpZW50X2lkIjoiMTIzNDI0MjM0MjM0MSIsImF1dGhfdXJpIjoiaHR0cHM6Ly9hY2NvdW50cy5nb29nbGUuY29tL28vb2F1dGgyL2F1dGgiLCJ0b2tlbl91cmkiOiJodHRwczovL29hdXRoMi5nb29nbGVhcGlzLmNvbS90b2tlbiIsImF1dGhfcHJvdmlkZXJfeDUwOV9jZXJ0X3VybCI6Imh0dHBzOi8vd3d3Lmdvb2dsZWFwaXMuY29tL29hdXRoMi92MS9jZXJ0cyIsImNsaWVudF94NTA5X2NlcnRfdXJsIjoiaHR0cHM6Ly93d3cuZ29vZ2xlYXBpcy5jb20vcm9ib3QvdjEvbWV0YWRhdGEveDUwOS9hc3NhZGphc2hkLmlhbS5nc2VydmljZWFjY291bnQuY29tIn0="
$
$
$ jq --compact-output --raw-output '@base64' <<< "$test" | base64 --decode
{"type":"service_account","project_id":"projectid234","private_key_id":"aasdadsxzce5","private_key":"-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCiWjA5+xF6gsGC\nOknIL9DOCQpV2LRXYPEKl5+mXuN45vwqh6QvG4lw/Hi7EJhtAn+FQy7+yOQYrw3l\nQ2CpxDotT+PT2OuQ6LVbc/F+SblPlrK3B+8aEMo57PZ+gnwMcQ7+ofPnzC635uUP\npOG0idMTK\n-----END PRIVATE KEY-----\n","client_email":"clinetmail@asdasd3435.iam.gserviceaccount.com","client_id":"1234242342341","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_x509_cert_url":"https://www.googleapis.com/robot/v1/metadata/x509/assadjashd.iam.gserviceaccount.com"}%
$
$
t3psigkw

t3psigkw2#

我不确定是否理解base64的用法,但简单的tr应该可以使用,例如:

$ var2=$(echo ${var1} | tr -d '\n')              # ${var1} is not wrapped in double quotes so repetitive white space is squeezed to single spaces

$ echo "${var2}"
{ "type": "service_account", "project_id": "projectid234", "private_key_id": "aasdadsxzce5", "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCiWjA5+xF6gsGC\nOknIL9DOCQpV2LRXYPEKl5+mXuN45vwqh6QvG4lw/Hi7EJhtAn+FQy7+yOQYrw3l\nQ2CpxDotT+PT2OuQ6LVbc/F+SblPlrK3B+8aEMo57PZ+gnwMcQ7+ofPnzC635uUP\npOG0idMTK\n-----END PRIVATE KEY-----\n", "client_email": "clinetmail@asdasd3435.iam.gserviceaccount.com", "client_id": "1234242342341", "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://oauth2.googleapis.com/token", "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/assadjashd.iam.gserviceaccount.com" }

或者像William Pursell在评论中指出的那样,不要用引号括住${var1},也可以去掉od调用:

$ var2=$(echo ${var1})                           # the unquoted ${var1} also removes linefeeds so no need for tr

$ echo "${var2}"
{ "type": "service_account", "project_id": "projectid234", "private_key_id": "aasdadsxzce5", "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCiWjA5+xF6gsGC\nOknIL9DOCQpV2LRXYPEKl5+mXuN45vwqh6QvG4lw/Hi7EJhtAn+FQy7+yOQYrw3l\nQ2CpxDotT+PT2OuQ6LVbc/F+SblPlrK3B+8aEMo57PZ+gnwMcQ7+ofPnzC635uUP\npOG0idMTK\n-----END PRIVATE KEY-----\n", "client_email": "clinetmail@asdasd3435.iam.gserviceaccount.com", "client_id": "1234242342341", "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://oauth2.googleapis.com/token", "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/assadjashd.iam.gserviceaccount.com" }

或者,如果您希望保留所有多余白色,则可以在传递到tr之前将${var1}括在双引号中,例如:

$ var2=$(echo "${var1}" | tr -d '\n')            # ${var1} is wrapped in double quotes to maintain all white space

$ echo "${var2}"
{      "type": "service_account",      "project_id": "projectid234",      "private_key_id": "aasdadsxzce5",      "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCiWjA5+xF6gsGC\nOknIL9DOCQpV2LRXYPEKl5+mXuN45vwqh6QvG4lw/Hi7EJhtAn+FQy7+yOQYrw3l\nQ2CpxDotT+PT2OuQ6LVbc/F+SblPlrK3B+8aEMo57PZ+gnwMcQ7+ofPnzC635uUP\npOG0idMTK\n-----END PRIVATE KEY-----\n",      "client_email": "clinetmail@asdasd3435.iam.gserviceaccount.com",      "client_id": "1234242342341",      "auth_uri": "https://accounts.google.com/o/oauth2/auth",      "token_uri": "https://oauth2.googleapis.com/token",      "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",      "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/assadjashd.iam.gserviceaccount.com"    }
7uhlpewt

7uhlpewt3#

作为一个快速的过程,您真的应该能够通过bash中的内置参数扩展来实现这一点。
最简单的:

$: echo "$var1" # this should present it exactly as you saved it
{
      "type": "service_account",
      "project_id": "projectid234",
      "private_key_id": "aasdadsxzce5",
      "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCiWjA5+xF6gsGC\nOknIL9DOCQpV2LRXYPEKl5+mXuN45vwqh6QvG4lw/Hi7EJhtAn+FQy7+yOQYrw3l\nQ2CpxDotT+PT2OuQ6LVbc/F+SblPlrK3B+8aEMo57PZ+gnwMcQ7+ofPnzC635uUP\npOG0idMTK\n-----END PRIVATE KEY-----\n",
      "client_email": "clinetmail@asdasd3435.iam.gserviceaccount.com",
      "client_id": "1234242342341",
      "auth_uri": "https://accounts.google.com/o/oauth2/auth",
      "token_uri": "https://oauth2.googleapis.com/token",
      "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
      "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/assadjashd.iam.gserviceaccount.com"
    }

$: echo "${var1//$'\n'/}" # this should strip out the newlines
{      "type": "service_account",      "project_id": "projectid234",      "private_key_id": "aasdadsxzce5",      "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCiWjA5+xF6gsGC\nOknIL9DOCQpV2LRXYPEKl5+mXuN45vwqh6QvG4lw/Hi7EJhtAn+FQy7+yOQYrw3l\nQ2CpxDotT+PT2OuQ6LVbc/F+SblPlrK3B+8aEMo57PZ+gnwMcQ7+ofPnzC635uUP\npOG0idMTK\n-----END PRIVATE KEY-----\n",      "client_email": "clinetmail@asdasd3435.iam.gserviceaccount.com",      "client_id": "1234242342341",      "auth_uri": "https://accounts.google.com/o/oauth2/auth",      "token_uri": "https://oauth2.googleapis.com/token",      "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",      "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/assadjashd.iam.gserviceaccount.com"    }

假设您还想用更复杂的模式挤出多余的空格,您可能会尝试使用正则表达式......这是行不通的,因为参数解析实际上使用的是w globbing,而正则表达式 * 本身 *......但请尝试shopt for extended globbing -

$: shopt -s extglob # c.f. https://mywiki.wooledge.org/glob#extglob
$: echo "${var1//$'\n'+( )/}" # remove a newline followed by one or more spaces
{"type": "service_account","project_id": "projectid234","private_key_id": "aasdadsxzce5","private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCiWjA5+xF6gsGC\nOknIL9DOCQpV2LRXYPEKl5+mXuN45vwqh6QvG4lw/Hi7EJhtAn+FQy7+yOQYrw3l\nQ2CpxDotT+PT2OuQ6LVbc/F+SblPlrK3B+8aEMo57PZ+gnwMcQ7+ofPnzC635uUP\npOG0idMTK\n-----END PRIVATE KEY-----\n","client_email": "clinetmail@asdasd3435.iam.gserviceaccount.com","client_id": "1234242342341","auth_uri": "https://accounts.google.com/o/oauth2/auth","token_uri": "https://oauth2.googleapis.com/token","auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs","client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/assadjashd.iam.gserviceaccount.com"}

编辑

有空白:这会将-----BEGIN PRIVATE KEY-----更改为-----BEGINPRIVATEKEY-----,我刚刚错过了它。
由于您的 * 数据 * 中似乎没有嵌入空格(这是我注意到的),您可以删除它们前面的换行符的 * 要求 *,并删除所有空格以使其更加紧凑。

$: echo "${var1//*($'\n')+( )/}"
{"type":"service_account","project_id":"projectid234","private_key_id":"aasdadsxzce5","private_key":"-----BEGINPRIVATEKEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCiWjA5+xF6gsGC\nOknIL9DOCQpV2LRXYPEKl5+mXuN45vwqh6QvG4lw/Hi7EJhtAn+FQy7+yOQYrw3l\nQ2CpxDotT+PT2OuQ6LVbc/F+SblPlrK3B+8aEMo57PZ+gnwMcQ7+ofPnzC635uUP\npOG0idMTK\n-----ENDPRIVATEKEY-----\n","client_email":"clinetmail@asdasd3435.iam.gserviceaccount.com","client_id":"1234242342341","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_x509_cert_url":"https://www.googleapis.com/robot/v1/metadata/x509/assadjashd.iam.gserviceaccount.com"}

同样,这会将-----BEGIN PRIVATE KEY-----更改为-----BEGINPRIVATEKEY-----
所以......在这种情况下,没有嵌入空格,您可以通过使用POSIX字符类删除所有空格(如顶部所示),在完全不扩展globbing的情况下获得最佳结果。:)

$: echo "${var1//[[:space:]]/}" # // means global replacement
{"type":"service_account","project_id":"projectid234","private_key_id":"aasdadsxzce5","private_key":"-----BEGINPRIVATEKEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCiWjA5+xF6gsGC\nOknIL9DOCQpV2LRXYPEKl5+mXuN45vwqh6QvG4lw/Hi7EJhtAn+FQy7+yOQYrw3l\nQ2CpxDotT+PT2OuQ6LVbc/F+SblPlrK3B+8aEMo57PZ+gnwMcQ7+ofPnzC635uUP\npOG0idMTK\n-----ENDPRIVATEKEY-----\n","client_email":"clinetmail@asdasd3435.iam.gserviceaccount.com","client_id":"1234242342341","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_x509_cert_url":"https://www.googleapis.com/robot/v1/metadata/x509/assadjashd.iam.gserviceaccount.com"}

相关问题