Unix:Filter&以CSV格式处理列数据

时间:2015-10-27 13:42:38

标签: linux csv unix awk sed

在Unix中,我如何转换CSV文件:

"0.4542703549","PRO000029D00QWERT","1.562"
"0.1585242685","PRO000030K00QWERT","4.833"
"0.4542703549","PRO000031K00QWERT","0.011"
"0.8917651385","PRO000032K00QWERT","3.057"

要:

"29","0.4542703549","1.562"
"30","0.1585242685","4.833"
"31","0.4542703549","0.011"
"32","0.8917651385","3.057"

本质上,我想从第二列(数字1-9除外)中删除所有内容,然后交换第1列和第1列的位置。 2.

提前感谢您的帮助..

1 个答案:

答案 0 :(得分:2)

最简单的用sed

sed 's/\("[^"]*"\),"PRO0*\([0-9]\{1,\}\)[^"]*"/\"\2",\1/' YourFile

替代

awk -F ',' -v 'quote="' '{print quote substr($2,5,6) + 0 quote "," $1 "," $3 }' YourFile