我有一个 CSV 文件,如下所示:
keyuat,carsim,logs-keyuat-carsim
lowuat,carsimserver,logs-lowuat-carsimserver
utils,dash,logs-utils-dash
utils,lifecycle,logs-utils-lifecycle
utils,lifecycle-nodejs,logs-utils-lifecycle-nodejs
workshop,cashier,logs-workshop-cashier
workshop,jfrog-dotnet,logs-workshop-jfrog-dotnet
workshop,labelsengine,logs-workshop-labelsengine
基于此 CSV 文件,我正在尝试运行两个必须一起执行的命令:
oc project $1
oc patch dc $2 -p '{"metadata":{"labels":{"logentries":"$3"}}}'
使用上面的真实示例,命令将是:
oc project keyuat
oc patch dc carsim -p '{"metadata":{"labels":{"logentries":"logs-keyuat-carsim"}}}'
我一直在尝试使用 awk,但我总是发现特殊字符或 /r 无效字符的问题,我没有看到。如果不是system
我使用print
,我会看到我的一些字符在行首重叠而不是在最后添加:
awk -F , '{ cmd="oc project " $1 "\;" "\n" "oc patch dc " $2 " \-p '\''\{\"metadata\"\:\{\"labels\"\:\{\"logentries\"\:\"" $3"\""; print(cmd) }' ./csv/labels.csv
"}}}oject keyuat; oc patch dc carsim -p '{"metadata":{"labels":{"logentries":"logs-keyuat-carsim
"}}}oject lowuat; oc patch dc carsimserver -p '{"metadata":{"labels":{"logentries":"logs-keyuat-carsimserver
"}}}oject utils; oc patch dc dash -p '{"metadata":{"labels":{"logentries":"logs-utils-dash
awk -F , '{ cmd="oc project " $1 "\;" "oc patch dc " $2 " -p '\''\{\"metadata\"\:\{\"labels\"\:\{\"logentries\"\:\"" $3 "\"\}\}\}'\''"; system(cmd) }' ./csv/labels.csv
awk: cmd. line:1: warning: escape sequence `\;' treated as plain `;'
awk: cmd. line:1: warning: escape sequence `\{' treated as plain `{'
awk: cmd. line:1: warning: escape sequence `\:' treated as plain `:'
awk: cmd. line:1: warning: escape sequence `\}' treated as plain `}'
Already on project "keyuat" on server "https://test-ocp.exampleusage.eu:443".
Error from server (BadRequest): invalid character '\r' in string literal
Already on project "lowuat" on server "https://test-ocp.exampleusage.eu:443".
Error from server (BadRequest): invalid character '\r' in string literal
Now using project "utils" on server "https://test-ocp.exampleusage.eu:443".
Error from server (BadRequest): invalid character '\r' in string literal
如何更正此脚本?