使用awk控制在文件中插入列的位置

时间:2016-05-11 21:41:04

标签: bash awk

这是我的档案:

$ cat -v 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"

此命令在末尾添加一列
NR==1{$7=q"Data_Volume_MB"q}在第1行的末尾添加列名。
NR>1{$7=$4; gsub(/"/,"",$7); $7= q $7/(1024*1024)q}1在第2行和第2行添加了一列

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' test1
"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"

如何控制插入列的位置?让我说我想在第二列之前插入它? NR==1{$(NF-1)=q"Data_Volume_MB"q FS $(NF-1);}这适用于第1行,但我无法弄清楚如何为第2行和更高版本执行此操作。任何人都可以建议我如何为line2和更高NR>1{$7=$4; gsub(/"/,"",$7); $7= q $7/(1024*1024)q}1

做这件事
$ awk -F, -v OFS=, -v q='"' 'NR==1{$(NF-1)=q"Data_Volume_MB"q FS $(NF-1);} NR>1{$7=$4; gsub(/"/,"",$7); $7= q $7/(1024*1024)q}1' test1
"Rec_Open_Date","MSISDN","IMEI","Data_Volume_Bytes","Device_Manufacturer","Data_Volume_MB","Device_Model","Product_Description"
"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"

对于第2行和更高版本我尝试了很多尝试,但它总是略微偏离。 e.g:

NR>1{$(NF-1)=$4; gsub(/"/,"",$(NF-1)); $(NF-1)= q $(NF-1)/(1024*1024)q FS $(NF-1);}1

EDIT1

这是我出错的方式

$ awk -F, -v OFS=, -v q='"' 'NR==1{$(NF-1)=q"Data_Volume_MB"q FS $(NF-1);} NR>1{$(NF-1)=$4; gsub(/"/,"",$(NF-1)); $(NF-1)= q $(NF-1)/(1024*1024)q FS $(NF-1);}1' test1 | head -n1 | tr ',' '\n'| cat -n
     1  "Rec_Open_Date"
     2  "MSISDN"
     3  "IMEI"
     4  "Data_Volume_Bytes"
     5  "Device_Manufacturer"
     6  "Data_Volume_MB"
     7  "Device_Model"
     8  "Product_Description"
$ awk -F, -v OFS=, -v q='"' 'NR==1{$(NF-1)=q"Data_Volume_MB"q FS $(NF-1);} NR>1{$(NF-1)=$4; gsub(/"/,"",$(NF-1)); $(NF-1)= q $(NF-1)/(1024*1024)q FS $(NF-1);}1' test1 | head -n2 | tail -n1 | tr ',' '\n'| cat -n
     1  "2015-10-06"
     2  "427"
     3  "060"
     4  "137765"
     5  "Samsung Korea"
     6  "0.131383"
     7  137765
     8  "$39 Plan"

@Etan

下面的答案

答案1 -

$ awk -F, -v OFS=, -v q='"' 'NR==1{$(NF-1)=q"Data_Volume_MB"q FS $(NF-1);} NR>1{n=$4; gsub(/"/,"",n); $(NF-1)= q n/(1024*1024)q FS $(NF-1);}1' test1 | head -n1 | tr ',' '\n' | cat -n
     1  "Rec_Open_Date"
     2  "MSISDN"
     3  "IMEI"
     4  "Data_Volume_Bytes"
     5  "Device_Manufacturer"
     6  "Data_Volume_MB"
     7  "Device_Model"
     8  "Product_Description"
$ awk -F, -v OFS=, -v q='"' 'NR==1{$(NF-1)=q"Data_Volume_MB"q FS $(NF-1);} NR>1{n=$4; gsub(/"/,"",n); $(NF-1)= q n/(1024*1024)q FS $(NF-1);}1' test1 | head -n2 | tail -n1  | tr ',' '\n' | cat -n
     1  "2015-10-06"
     2  "427"
     3  "060"
     4  "137765"
     5  "Samsung Korea"
     6  "0.131383"
     7  "Samsung SM-G900I"
     8  "$39 Plan"
$

答案2 -

在第二个最后一列之后插入它(从第二个最后一列开始)

$ awk -F, -v OFS=, -v q='"' 'NR==1{$(NF-1)=$(NF-1) FS q"Data_Volume_MB"q} NR>1{n=$4; gsub(/"/,"",n); $(NF-1)= $(NF-1) FS q n/(1024*1024)q}1' test1 | head -n1 | tr ',' '\n' | cat -n
     1  "Rec_Open_Date"
     2  "MSISDN"
     3  "IMEI"
     4  "Data_Volume_Bytes"
     5  "Device_Manufacturer"
     6  "Device_Model"
     7  "Data_Volume_MB"
     8  "Product_Description"
$ awk -F, -v OFS=, -v q='"' 'NR==1{$(NF-1)=$(NF-1) FS q"Data_Volume_MB"q} NR>1{n=$4; gsub(/"/,"",n); $(NF-1)= $(NF-1) FS q n/(1024*1024)q}1' test1 | head -n2 | tail -n1 | tr ',' '\n' | cat -n
     1  "2015-10-06"
     2  "427"
     3  "060"
     4  "137765"
     5  "Samsung Korea"
     6  "Samsung SM-G900I"
     7  "0.131383"
     8  "$39 Plan"

答案3 -

在第二个最后一列之后插入它(从最后一列开始)

$ awk -F, -v OFS=, -v q='"' 'NR==1{$NF=q"Data_Volume_MB"q FS $NF} NR>1{n=$4; gsub(/"/,"",n); $NF= q n/(1024*1024)q FS $NF;}1' test1 | head -n1 | tr ',' '\n' | cat -n
     1  "Rec_Open_Date"
     2  "MSISDN"
     3  "IMEI"
     4  "Data_Volume_Bytes"
     5  "Device_Manufacturer"
     6  "Device_Model"
     7  "Data_Volume_MB"
     8  "Product_Description"
$ awk -F, -v OFS=, -v q='"' 'NR==1{$NF=q"Data_Volume_MB"q FS $NF} NR>1{n=$4; gsub(/"/,"",n); $NF= q n/(1024*1024)q FS $NF;}1' test1 | head -n2 | tail -n1 | tr ',' '\n' | cat -n
     1  "2015-10-06"
     2  "427"
     3  "060"
     4  "137765"
     5  "Samsung Korea"
     6  "Samsung SM-G900I"
     7  "0.131383"
     8  "$39 Plan"

1 个答案:

答案 0 :(得分:1)

您将$(NF-1)上的值替换为您在操作中执行的第一项操作。然后,您尝试使用$(NF-1),就像它仍然具有原始值一样。

那不行。您需要将值保存到另一个变量,对其进行操作,然后将$(NF-1)设置为新值。

NR>1{n=$4; gsub(/"/,"",n); $(NF-1)= q n/(1024*1024)q FS $(NF-1)}1

另外,要将新值用作倒数第二列,您需要将放在之后的第二列到最后一列中(或操作最后一列并将其放在它之前)。

NR>1{n=$4; gsub(/"/,"",n); $(NF-1)= $(NF-1) FS q n/(1024*1024)q}1

NR>1{n=$4; gsub(/"/,"",n); $NF= q n/(1024*1024)q FS $NF;}1