我有檔案 data.csv,其中包含有關一家商店的客戶體驗和商店總支出的資訊。每個客戶都會根據他們的客戶體驗給出分數,因此資料集如下所示:
Ranking Total Spent
9.5 1245
5 500.58
7.8 1000.69
3 200
6.2 412.45
我想創建一個名為“體驗”的新列,其值取決于“排名”列。例如:
ranking >= 8 the new column value will be the string "Very satisfied"
ranking >= 6 && ranking < 8 the new column value will be "Satisfied"
ranking >= 5 && ranking < 6 the new column value will be "Neutral"
ranking >= 3 && ranking < 5 the new column value will be "Bad"
ranking >= 0 && ranking < 3 the new column value will be "Horrible"
所以想要的輸出是:
Ranking Total Spent Experience
9.5 1245 Very satisfied
5 500.58 Neutral
7.8 1000.69 Satisfied
3 200 Bad
6.2 412.45 Satisfied
我嘗試使用以下代碼但無法正常作業:
awk -F,'NR==1{$3="Experience";print;next}
$1>=8 {print $0, "Very satisfied";next}
$1>=6 && $1<8 {print $0, "Satisfied";next}
$1>=5 && $1<6 {print $0, "Neutral";next}
$1>=3 && $1<5 {print $0, "Bad";next}
$1>=0 && $1<3 {print $0, "Horrible";next}' data.csv
uj5u.com熱心網友回復:
你真的很親近。只是OFS失蹤。為了避免轉義引號,您可以創建這樣的腳本
#! /usr/bin/awk -f
NR==1 {FS=","; OFS="\t"; $2="Experience";print;next}
$1>=8 {print $0, "Very satisfied";next}
$1>=6 && $1<8 {print $0, "Satisfied";next}
$1>=5 && $1<6 {print $0, "Neutral";next}
$1>=3 && $1<5 {print $0, "Bad";next}
$1>=0 && $1<3 {print $0, "Horrible";next}
賦予權限
chmod x myscript
并運行它
./myscript data.csv
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/485019.html
上一篇:有人可以解釋這個sed命令嗎?
下一篇:JQ原始文本到雙引號鍵值對
