如何在文件中添加新列

发布于 2024-09-26 03:17:48 字数 716 浏览 5 评论 0原文

我有这样的文件,其中包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

红尘作伴 2024-10-03 03:17:48
BEGIN{
    FS=","
}

NF==5 { print; }
NF==4 { printf("%s,,%s,%s,%s\n", $1, $2, $3, $4); }
BEGIN{
    FS=","
}

NF==5 { print; }
NF==4 { printf("%s,,%s,%s,%s\n", $1, $2, $3, $4); }
怪我鬧 2024-10-03 03:17:48

更短的路

awk -F"," 'NF<5{sub(",",",,")}1' file

shorter way

awk -F"," 'NF<5{sub(",",",,")}1' file
毅然前行 2024-10-03 03:17:48

使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文