提问人:isaacj94 提问时间:7/4/2023 更新时间:7/4/2023 访问量:85
在文件中执行 yq replace 命令时如何转义连字符
How to escape hyphens when performing yq replace in file command
问:
目前,我正在尝试使用 yq 替换 yaml 文件中变量的内容。这样做时,我收到一个无效的输入文本错误,自从第一次遇到这个错误以来,我注意到它不欣赏变量中的连字符,因此它杀死了命令。下面是我所看到的示例
[ec2-user@ip-146-69-172-7 example]$ yq -e -i ".variables.RANCHER_PROJECT_ID = $(yq -r -oy .id rancher_api_project_creation_response.json)" example-variables.yml -vvv
19:33:39 processArgs [DEBU] processed args: [.variables.RANCHER_PROJECT_ID = c-tt27c:p-4bp9x example-variables.yml]
19:33:39 maybeFile [DEBU] checking '.variables.RANCHER_PROJECT_ID = c-tt27c:p-4bp9x' is a file
19:33:39 maybeFile [DEBU] error: stat .variables.RANCHER_PROJECT_ID = c-tt27c:p-4bp9x: no such file or directory
19:33:39 maybeFile [DEBU] result: false
19:33:39 processArgs [DEBU] assuming expression is '.variables.RANCHER_PROJECT_ID = c-tt27c:p-4bp9x'
19:33:39 FormatFromFilename [DEBU] checking file extension 'example-variables.yml' for auto format detection
19:33:39 FormatFromFilename [DEBU] detected format 'yml'
19:33:39 initCommand [DEBU] Using input format yml
19:33:39 initCommand [DEBU] Using output format yml
19:33:39 CreateTempFile [DEBU] WriteInPlaceHandler: writing to tempfile: /tmp/temp1936411158
19:33:39 ParseExpression [DEBU] Parsing expression: [.variables.RANCHER_PROJECT_ID = c-tt27c:p-4bp9x]
19:33:39 func1 [DEBU] PathToken variables
19:33:39 func1 [DEBU] PathToken RANCHER_PROJECT_ID
19:33:39 func1 [DEBU] assignOpToken =
Error: 1:33: invalid input text "c-tt27c:p-4bp9x"
这是我测试一些基本文本以找出导致问题的连字符的示例
[ec2-user@ip-146-69-172-7 example]$ yq -e -i ".variables.RANCHER_PROJECT_ID = $(echo "12-34:56-78")" example-variables.yml -vvv
20:03:31 processArgs [DEBU] processed args: [.variables.RANCHER_PROJECT_ID = 12-34:56-78 example-variables.yml]
20:03:31 maybeFile [DEBU] checking '.variables.RANCHER_PROJECT_ID = 12-34:56-78' is a file
20:03:31 maybeFile [DEBU] error: stat .variables.RANCHER_PROJECT_ID = 12-34:56-78: no such file or directory
20:03:31 maybeFile [DEBU] result: false
20:03:31 processArgs [DEBU] assuming expression is '.variables.RANCHER_PROJECT_ID = 12-34:56-78'
20:03:31 FormatFromFilename [DEBU] checking file extension 'example-variables.yml' for auto format detection
20:03:31 FormatFromFilename [DEBU] detected format 'yml'
20:03:31 initCommand [DEBU] Using input format yml
20:03:31 initCommand [DEBU] Using output format yml
20:03:31 CreateTempFile [DEBU] WriteInPlaceHandler: writing to tempfile: /tmp/temp3358661156
20:03:31 ParseExpression [DEBU] Parsing expression: [.variables.RANCHER_PROJECT_ID = 12-34:56-78]
20:03:31 func1 [DEBU] PathToken variables
20:03:31 func1 [DEBU] PathToken RANCHER_PROJECT_ID
20:03:31 func1 [DEBU] assignOpToken =
20:03:31 createValueOperation [DEBU] creating value op for string 12
20:03:31 createValueOperation [DEBU] creating value op for string -34
20:03:31 createValueOperation [DEBU] creating value op for string 56
20:03:31 createValueOperation [DEBU] creating value op for string -78
20:03:31 handleToken [DEBU] processing variables (55)
20:03:31 handleToken [DEBU] adding token to the fixed list
20:03:31 handleToken [DEBU] adding pipe because the next thing is traverse
20:03:31 handleToken [DEBU] processing RANCHER_PROJECT_ID (55)
20:03:31 handleToken [DEBU] adding token to the fixed list
20:03:31 handleToken [DEBU] processing ASSIGN (40)
20:03:31 handleToken [DEBU] adding token to the fixed list
20:03:31 handleToken [DEBU] processing 12 (int64) (50)
20:03:31 handleToken [DEBU] adding token to the fixed list
20:03:31 handleToken [DEBU] processing -34 (int64) (50)
20:03:31 handleToken [DEBU] adding token to the fixed list
20:03:31 handleToken [DEBU] processing CREATE_MAP (15)
20:03:31 handleToken [DEBU] tokenIsOpType: createMapOpType
20:03:31 handleToken [DEBU] adding token to the fixed list
20:03:31 handleToken [DEBU] tokenIsOpType: createMapOpType
20:03:31 handleToken [DEBU] processing 56 (int64) (50)
20:03:31 handleToken [DEBU] adding token to the fixed list
20:03:31 handleToken [DEBU] processing -78 (int64) (50)
20:03:31 handleToken [DEBU] adding token to the fixed list
20:03:31 ConvertToPostfix [DEBU] postfix processing currentToken variables (55)
20:03:31 ConvertToPostfix [DEBU] put variables (55) onto the opstack
20:03:31 ConvertToPostfix [DEBU] postfix processing currentToken SHORT_PIPE (45)
20:03:31 popOpToResult [DEBU] popped variables (55) from opstack to results
20:03:31 ConvertToPostfix [DEBU] put SHORT_PIPE (45) onto the opstack
20:03:31 ConvertToPostfix [DEBU] postfix processing currentToken RANCHER_PROJECT_ID (55)
20:03:31 ConvertToPostfix [DEBU] put RANCHER_PROJECT_ID (55) onto the opstack
20:03:31 ConvertToPostfix [DEBU] postfix processing currentToken ASSIGN (40)
20:03:31 popOpToResult [DEBU] popped RANCHER_PROJECT_ID (55) from opstack to results
20:03:31 popOpToResult [DEBU] popped SHORT_PIPE (45) from opstack to results
20:03:31 ConvertToPostfix [DEBU] put ASSIGN (40) onto the opstack
20:03:31 ConvertToPostfix [DEBU] postfix processing currentToken 12 (int64) (50)
20:03:31 ConvertToPostfix [DEBU] put 12 (int64) (50) onto the opstack
20:03:31 ConvertToPostfix [DEBU] postfix processing currentToken -34 (int64) (50)
20:03:31 ConvertToPostfix [DEBU] put -34 (int64) (50) onto the opstack
20:03:31 ConvertToPostfix [DEBU] postfix processing currentToken CREATE_MAP (15)
20:03:31 popOpToResult [DEBU] popped -34 (int64) (50) from opstack to results
20:03:31 popOpToResult [DEBU] popped 12 (int64) (50) from opstack to results
20:03:31 popOpToResult [DEBU] popped ASSIGN (40) from opstack to results
20:03:31 ConvertToPostfix [DEBU] put CREATE_MAP (15) onto the opstack
20:03:31 ConvertToPostfix [DEBU] postfix processing currentToken 56 (int64) (50)
20:03:31 ConvertToPostfix [DEBU] put 56 (int64) (50) onto the opstack
20:03:31 ConvertToPostfix [DEBU] postfix processing currentToken -78 (int64) (50)
20:03:31 ConvertToPostfix [DEBU] put -78 (int64) (50) onto the opstack
20:03:31 ConvertToPostfix [DEBU] postfix processing currentToken )
20:03:31 popOpToResult [DEBU] popped -78 (int64) (50) from opstack to results
20:03:31 popOpToResult [DEBU] popped 56 (int64) (50) from opstack to results
20:03:31 popOpToResult [DEBU] popped CREATE_MAP (15) from opstack to results
20:03:31 ConvertToPostfix [DEBU] opstackLen: 0
20:03:31 ConvertToPostfix [DEBU] PostFix Result:
20:03:31 ConvertToPostfix [DEBU] > variables
20:03:31 ConvertToPostfix [DEBU] > RANCHER_PROJECT_ID
20:03:31 ConvertToPostfix [DEBU] > SHORT_PIPE
20:03:31 ConvertToPostfix [DEBU] > -34 (int64)
20:03:31 ConvertToPostfix [DEBU] > 12 (int64)
20:03:31 ConvertToPostfix [DEBU] > ASSIGN
20:03:31 ConvertToPostfix [DEBU] > -78 (int64)
20:03:31 ConvertToPostfix [DEBU] > 56 (int64)
20:03:31 ConvertToPostfix [DEBU] > CREATE_MAP
20:03:31 createExpressionTree [DEBU] pathTree variables
20:03:31 createExpressionTree [DEBU] pathTree RANCHER_PROJECT_ID
20:03:31 createExpressionTree [DEBU] pathTree SHORT_PIPE
20:03:31 createExpressionTree [DEBU] pathTree -34 (int64)
20:03:31 createExpressionTree [DEBU] pathTree 12 (int64)
20:03:31 createExpressionTree [DEBU] pathTree ASSIGN
20:03:31 createExpressionTree [DEBU] pathTree -78 (int64)
20:03:31 createExpressionTree [DEBU] pathTree 56 (int64)
20:03:31 createExpressionTree [DEBU] pathTree CREATE_MAP
Error: bad expression, please check expression syntax
我只需要弄清楚如何强制表达式将连字符识别为新变量值的实际部分并以某种方式转义它们。
答: 暂无答案
评论