有一個包含大量行的 csv 檔案,小例子:
id,location_id,name,title,email,directorate
1,1, Amy lee,Singer,,
2,2,brad Pitt,Actor,,Production
3,5,Steven Spielberg,Producer,[email protected],Production
需要:
- 將名字和姓氏更改為大寫,例如 Brad Pitt, Amy Lee。
- 使用名字 姓氏的模式首字母創建電子郵件,全部小寫,@google.com 和location_id的值,例如 - [email protected], [email protected]
- 將其保存到新的 file.csv,具有相同的結構,例如:
id,location_id,name,title,email,directorate
1,1, Amy Lee,Singer,[email protected],
2,2,Brad Pitt,Actor,[email protected],Production
3,5,Steven Spielberg,Producer,[email protected],Production
我從創建一個陣列開始,用一堆 sed、awk 遍歷它,但它給了我隨機的結果。請給我建議,如何解決這個任務。
while read -ra array; do
for i in ${array[@]};
do
awk -F ',' '{print tolower(substr($3,1,1))$2$3"@google.com"}'
done
for i in ${array[@]};
do
awk -F "\"*,\"*" '{print $3}' | sed -e "s/\b\(.\)/\u\1/g"
done
done < file.csv
awk -F ',' '{print tolower(substr($3,1,1))$2$3"@google.com"}'作業不正確。
uj5u.com熱心網友回復:
使用 GNUsed
$ sed -E 's/([^,]*,([^,]*),) ?(([[:alpha:]])[^ ]* )(([^,]*),[^,]*,)[^,]*/\1\u\3\u\5\L\4\6\[email protected]/' input_file
id,location_id,name,title,email,directorate
1,1,Amy Lee,Singer,[email protected],
2,2,Brad Pitt,Actor,[email protected],Production
3,5,Steven Spielberg,Producer,[email protected],Production
uj5u.com熱心網友回復:
使用您展示的樣品,請嘗試以下操作awk。
awk '
BEGIN{ FS=OFS="," }
{
split($3,arr," ")
val=(substr($3,1,1) arr[2]"@google.com,")
$NF=tolower(val) $NF
val=""
}
1
' Input_file
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/527934.html
標籤:重击awksed
