这是linux系统中的csv文件。这是文件中的一行。
:/var/opt/data# vi data_2018_07.csv
LGPAR,DATA,61543261,98747321900,gprs,464623201315531,NRI,1.45890,0.000000,0.0000,,2016-01-12 01:50:58,2016-01-12 15:50:58,0,11,0,0,0,0,25250,NO,NO,NO,1056.gz
同样,我们在那个文件中有10000行。我们只想在
NO,xxxx.gz
之间的某个位置添加一个增量值(第24个值),例如:-LGPAR,DATA,61543261,98747321900,gprs,464623201315531,NRI,1.45890,0.000000,0.0000,,2016-01-12 01:50:58,2016-01-12 15:50:58,0,11,0,0,0,0,25250,NO,NO,NO,1, 1056.gz
LGRDR,DATA,61543261,98747321900,gprs,464623201315531,NRI,1.45890,0.000000,0.0000,,2016-01-12 01:50:58,2016-01-12 15:50:58,0,11,0,0,0,0,25250,NO,NO,NO,2, 1057.gz
我们可以在整个文件中使用一个小的shell脚本来处理所有行吗?
注意:每行的行数不同。
最佳答案
您可以使用这个awk
:
awk 'BEGIN{FS=OFS=","} {$24 = NR FS $24} 1' file.csv
LGPAR,DATA,61543261,98747321900,gprs,464623201315531,NRI,1.45890,0.000000,0.0000,,2016-01-12 01:50:58,2016-01-12 15:50:58,0,11,0,0,0,0,25250,NO,NO,NO,1,1056.gz
要将更改保存在同一文件中,请执行以下操作:
awk 'BEGIN{FS=OFS=","} {$24 = NR FS $24} 1' file.csv > tmp.csv && mv tmp.csv file.csv
关于linux - 将值从一列复制到另一列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51240892/