如何在csv文件中使用awk或sed将用冒号(:)分隔的字符串值分割到下一列。

问题描述 投票:0回答:1

我有下面的数据,我想把用冒号(:)分隔的值分割到下一列。

样本数据

htttp://example.com, 80
http://lookup/bin/search, 80
testecho345.unix.abc1200.org:8115,80
century.testing.external-abc03:6112,80
century.testing.external-abc03:6112,80
testecho345.unix.abc1200.org:8115,80
testecho345.unix.abc1200.org:8117,80

期望的产出

htttp://example.com, 80
http://lookup/bin/search, 80
testecho345.unix.abc1200.org,8115
century.testing.external-abc03,6112
testecho345.unix.abc1200.org,8117

注意:如果是http则打印80,https则打印443,如果URL中有任何数字,则打印该数字而不是80。

尝试了以下AWK命令,但没有得到预期的输出。

awk '{split($0,a,":"); print a[1],a[2]}'

同时也删除了重复的内容

先谢谢你的帮助。

shell csv awk sed
1个回答
3
投票
awk -F'[:,]' '!a[$0]++{print $1","$2}'

根据我对你的要求的最佳解释,这样就可以了。

  • 没有重复
  • 冒号后的字段变成了自己的CSV字段。
  • 末列

概念验证

$ awk -F'[:,]' '!a[$0]++{print $1","$2}' < /path/to/input
testecho345.unix.abc1200.org,8115
century.testing.external-abc03,6112
testecho345.unix.abc1200.org,8117

请注意,你想要的输出仍然显示重复,我猜想这是一个错误。


1
投票

这是我通过 sed 进行的尝试。它不能处理重复的内容。你可以很容易地使用一些Unix命令来消除重复,但这不是你所要求的。也许有人知道如何通过 sed 来实现?

sed 's/\(.*\):\(.*\),.*/\1,\2/'

$ sed 's/(.*/):\(.*/),.*/1,\2' file_nametestecho345.unix.abc1200.org,8115century.testing.external-abc03,6112century.testing.external-abc03,6112testecho345.unix.abc1200.org,8115testecho345.unix.abc1200.org,8117$。
© www.soinside.com 2019 - 2024. All rights reserved.