这是我的档案
$ cat test1
"Rec_Open_Date","MSISDN","IMEI","Data_Volume_Bytes","Device_Manufacturer","Device_Model","Product_Description"
"2015-10-06","427","060","137765","Samsung Korea","Samsung SM-G900I","$39 Plan"
"2015-10-06","592","620","0","Apple Inc","Apple iPhone 6 (A1586)","PREPAY STD - TRIAL - #16"
"2015-10-06","007","290","0","Apple Inc","Apple iPhone 6 (A1586)","PREPAY PLUS - $0 -"
"2015-10-06","592","050","48836832","Apple Inc","Apple iPhone 5S (A1530)","Talk and Text Connect Flexi Plan"
"2015-10-06","409","720","113755347","Samsung Korea","Samsung SM-G360G","$29 CARRYOVER PLAN"
"2015-10-06","742","620","19840943","Apple Inc","Apple iPhone S (A1530)","PREPAY STD - $0 - #2"
"2015-10-06","387","180","0","HUAWEI Technologies Co Ltd","HUAWEI HUAWEI G526-L11","PREPAY STD - $1 - #4"
"2015-10-06","731","570","2258243","Samsung Korea","Samsung SM-N910U","Business Freedom"
"2015-10-06","556","910","13332272","Samsung Korea","Samsung GT-I9505","$49 Plan"
此命令在末尾添加一列。
$ awk -F"," 'BEGIN { OFS = "," } {$7=($4); print}' test1
"Rec_Open_Date","MSISDN","IMEI","Data_Volume_Bytes","Device_Manufacturer","Device_Model","Data_Volume_Bytes"
"2015-10-06","427","060","137765","Samsung Korea","Samsung SM-G900I","137765"
"2015-10-06","592","620","0","Apple Inc","Apple iPhone 6 (A1586)","0"
如何获得以下输出:为简单起见,这只是$7=($4/1)
。
"Rec_Open_Date","MSISDN","IMEI","Data_Volume_Bytes","Device_Manufacturer","Device_Model","Data_Volume_Bytes"
"2015-10-06","427","060","137765","Samsung Korea","Samsung SM-G900I","137765"
"2015-10-06","592","620","0","Apple Inc","Apple iPhone 6 (A1586)","0"
我真正希望能够做的是$7=$4/10124/1024
,但由于某种原因,我无法让该部门工作。
我试过这个,但我只得到$ 7 =等于零。
awk -F"," 'BEGIN { OFS = "," } {$7=($4/1024); print}' test1
"Rec_Open_Date","MSISDN","IMEI","Data_Volume_Bytes","Device_Manufacturer","Device_Model",0
"2015-10-06","427","060","137765","Samsung Korea","Samsung SM-G900I",0
EDIT1:
我是如何更改第7列中col标头的名称的。我不确定使用\"
是将其保留在双引号内的最佳或唯一方法。
$ awk -F"," '{ OFS = "," } NR<=1{$7="\"Data_Volume_MB\""; print}' test1
"Rec_Open_Date","MSISDN","IMEI","Data_Volume_Bytes","Device_Manufacturer","Device_Model","Data_Volume_MB"
EDIT2
这是我想要的,但涉及casting from a string to an int in awk substr($4,2,(length($4)-2))
。但是答案不是双引号0.131383
,为了统一我如何实现这个目标?
$ awk -F"," '{ OFS = "," } NR>1{$7=substr($4,2,(length($4)-2))/1024/1024; print}' test1
"2015-10-06","427","060","137765","Samsung Korea","Samsung SM-G900I",0.131383
"2015-10-06","592","620","0","Apple Inc","Apple iPhone 6 (A1586)",0
"2015-10-06","007","290","0","Apple Inc","Apple iPhone 6 (A1586)",0
"2015-10-06","592","050","48836832","Apple Inc","Apple iPhone 5S (A1530)",46.5744
答案 0 :(得分:1)
你正走在正确的轨道上,只有一些小问题
$ awk -F, -v OFS=, -v q='"' 'NR==1{$7=q"Data_Volume_MB"q}
NR>1{$7=$4; gsub(/"/,"",$7); $7= q $7/(1024*1024)q}1' file
"Rec_Open_Date","MSISDN","IMEI","Data_Volume_Bytes","Device_Manufacturer","Device_Model","Data_Volume_MB"
"2015-10-06","427","060","137765","Samsung Korea","Samsung SM-G900I","0.131383"
"2015-10-06","592","620","0","Apple Inc","Apple iPhone 6 (A1586)","0"
"2015-10-06","007","290","0","Apple Inc","Apple iPhone 6 (A1586)","0"
"2015-10-06","592","050","48836832","Apple Inc","Apple iPhone 5S (A1530)","46.5744"
"2015-10-06","409","720","113755347","Samsung Korea","Samsung SM-G360G","108.486"
"2015-10-06","742","620","19840943","Apple Inc","Apple iPhone S (A1530)","18.9218"
"2015-10-06","387","180","0","HUAWEI Technologies Co Ltd","HUAWEI HUAWEI G526-L11","0"
"2015-10-06","731","570","2258243","Samsung Korea","Samsung SM-N910U","2.15363"
"2015-10-06","556","910","13332272","Samsung Korea","Samsung GT-I9505","12.7146"'
-v q='"'
定义变量而不是转义引号
gsub(/"/,"",$7)
删除引号
$7= q $7/(1024*1024)q
进行划分并添加引号