我有下面的数据,我想把用冒号(:)分隔的值分割到下一列。
样本数据
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]}'
同时也删除了重复的内容
先谢谢你的帮助。
awk -F'[:,]' '!a[$0]++{print $1","$2}'
根据我对你的要求的最佳解释,这样就可以了。
$ 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
请注意,你想要的输出仍然显示重复,我猜想这是一个错误。
这是我通过 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$。