Shell

AWK 從帶有跳轉行的 CSV 文件中的項目啟動作業系統命令

  • September 14, 2022

我有一個 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 文件,我正在嘗試執行 2 個必須一起執行的命令

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 無效字元的問題,如果我使用“列印”而不是“系統”,我看到我的一些字元在開頭重疊該行而不是最後添加:

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

任何人都可以指出我的錯誤,拜託……讓我發瘋

在 awk 中,您可以使用以下命令輕鬆列印單引號:\047

你可以試試(我只是在這裡輸入,我現在無法測試……我希望它是正確的!)

# if needed: take out "^M" from the input file
tr -d '\015' <inputfile.csv >inputfile_sanitized.csv

# then parse it with:
awk -F',' '
 { cmd1="oc project " $1 
   cmd2="oc patch dc " $2 " -p \047{\"metadata\":{\"labels\":{\"logentries\":\"" $3 "\"}}}\047"
   ## once sure: you can here do: system(cmd1 " ; " cmd2); 
   print cmd1 " ; " cmd2
 }' inputfile_sanitized.csv

我會用

  • jq來生成 JSON - 這將是處理任何引用邊緣情況的最安全方法,並且
  • bash來構造命令——由於bash FAQ #50
json=$(jq -n -c --arg le "$3" '{metadata: {labels: {logentries: $le}}}')

# no `$` here, I'm building arrays
cmd1=(oc project "$1")
cmd2=(oc patch dc "$2" -p "$json")

# now, execute the commands
"${cmd1[@]}"
"${cmd2[@]}"

要檢查命令的內容:

$ declare -p cmd1 cmd2
declare -a cmd1=([0]="oc" [1]="project" [2]="keyuat")
declare -a cmd2=([0]="oc" [1]="patch" [2]="dc" [3]="carsin" [4]="-p" [5]="{\"metadata\":{\"labels\":{\"logentries\":\"logs-keyuat-carsim\"}}}")

引用自:https://unix.stackexchange.com/questions/717254