如何在文件中添加新列
我有这样的文件,其中包含 5 列。但我的文件中的某些行错误地有 4 列,例如在该文件的第 5 行和第 6 行中,您可以看到第二列丢失了。我想用空格替换丢失的第二列,而不用字段分隔符“,”干扰文件中的其他行。
11111,5323,6296,29-May-2010,1 22222,5323,6296,24-May-2010,1 33333,5323,6296,24-Jun-2010,1 44444,5323,6296,24-Jun-2010,1 55555,8061,15-Jul-2010,1 66666,6296,29-May-2010,1 77777,5323,6296,29-May-2010,1 88888,6296,29-May-2010,1 99999,6296,27-May-2010,1 10101,5323,6296,29-May-2010,1
输出我需要的。
11111,5323,6296,29-May-2010,1 22222,5323,6296,24-May-2010,1 33333,5323,6296,24-Jun-2010,1 44444,5323,6296,24-Jun-2010,1 55555,,8061,15-Jul-2010,1 66666,,6296,29-May-2010,1 77777,5323,6296,29-May-2010,1 88888,,6296,29-May-2010,1 99999,,6296,27-May-2010,1 10101,5323,6296,29-May-2010,1
I have file like this which contains 5 columns. But some rows in my file have 4 columns by mistake, for example in this file at the 5th and 6th rows, you can see that the second column is missing. I want to replace the missing 2nd column with a blank space without disturbing other rows in my file with field separator ",".
11111,5323,6296,29-May-2010,1 22222,5323,6296,24-May-2010,1 33333,5323,6296,24-Jun-2010,1 44444,5323,6296,24-Jun-2010,1 55555,8061,15-Jul-2010,1 66666,6296,29-May-2010,1 77777,5323,6296,29-May-2010,1 88888,6296,29-May-2010,1 99999,6296,27-May-2010,1 10101,5323,6296,29-May-2010,1
Output that I need.
11111,5323,6296,29-May-2010,1 22222,5323,6296,24-May-2010,1 33333,5323,6296,24-Jun-2010,1 44444,5323,6296,24-Jun-2010,1 55555,,8061,15-Jul-2010,1 66666,,6296,29-May-2010,1 77777,5323,6296,29-May-2010,1 88888,,6296,29-May-2010,1 99999,,6296,27-May-2010,1 10101,5323,6296,29-May-2010,1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更短的路
shorter way
使用 Vim,我会在 Ex-Mode 中输入以下行:
:%s/^(\d+),(\d+,\d+-\w+-\d+,\d)$/\1, ,\2/g
Using Vim I'd enter the following line in Ex-Mode:
:%s/^(\d+),(\d+,\d+-\w+-\d+,\d)$/\1, ,\2/g