30

我有一个需要使用 awk 处理的文本文件。

"5","1211274723","0","D","2"
"1","1211292921","0","A","2"
"5","1211295793","0","A","2"
"5","1211310146","0","A","2"
"5","1211310310","0","A","2"
"4","1211315271","0","A","2"
"5","1211318203","0","D","2"
"2","1211323658","0","A","2"
"5","1211329224","0","A","2"
"5","1211330064","0","A","2"


# cat testme.csv | awk -F',' '{print "set", $2, $3}'
set "1211274723" "0"
set "1211292921" "0"
set "1211295793" "0"
set "1211310146" "0"
set "1211310310" "0"
set "1211315271" "0"
set "1211318203" "0"
set "1211323658" "0"
set "1211329224" "0"
set "1211330064" "0"

唯一的问题是我不知道如何删除电话号码周围的引号。所以我的最终输出看起来像这样......

set 1211274723 "0"
set 1211292921 "0"
set 1211295793 "0"
set 1211310146 "0"
set 1211310310 "0"
set 1211315271 "0"
set 1211318203 "0"
set 1211323658 "0"
set 1211329224 "0"
set 1211330064 "0"
4

2 回答 2

60

您可以使用 gsub 功能:

awk -F',' '{gsub(/"/, "", $2); print "set", $2, $3}' testme.csv 
set 1211274723 "0"
set 1211292921 "0"
set 1211295793 "0"
set 1211310146 "0"
set 1211310310 "0"
set 1211315271 "0"
set 1211318203 "0"
set 1211323658 "0"
set 1211329224 "0"
set 1211330064 "0"
于 2013-10-20T07:16:16.453 回答
20

使用双引号作为字段分隔符;

 awk -F'"' '{print "set", $4, "\""$6"\""}' testme.csv
于 2013-10-20T07:16:44.890 回答