我有一个文件,其中有许多示例行:
1.2.4.0-1.2.4.255
1.2.8.0-1.2.8.255
1.9.96.105-1.9.96.105
1.9.102.251-1.9.102.251
1.9.106.186-1.9.106.186
1.16.0.0-1.19.255.255
1.55.241.140-1.55.241.140
每行是一个IP范围,但是某些行,例如1.9.96.105-1.9.96.105
仅具有一个IP。这些行应仅转换为1.9.96.105
,因此列表如下所示:
1.2.4.0-1.2.4.255
1.2.8.0-1.2.8.255
1.9.96.105
1.9.102.251
1.9.106.186
1.16.0.0-1.19.255.255
1.55.241.140
该怎么做?
答案 0 :(得分:3)
使用awk:
$ awk -F- '$1==$2{$2=""}1' file
1.2.4.0-1.2.4.255
1.2.8.0-1.2.8.255
1.9.96.105
1.9.102.251
1.9.106.186
1.16.0.0-1.19.255.255
1.55.241.140
即。将字段分隔符设置为-
,如果$1
和$2
相等,则仅打印第一个。
编辑:要摆脱该尾随空格,您可以:awk -F- -v OFS="" ...
,即。将输出字段分隔符OFS
设置为空(""
)或查看其他注释。
答案 1 :(得分:2)
在这种情况下,您可以使用带有sed的正则表达式
SELECT
os
FROM
servers s
LEFT OUTER JOIN
(SELECT
serial,
COUNT(serial) AS aantalinterventies
FROM
interventies) AS interventies USING (serial)
GROUP BY
os
HAVING
aantalinterventies/Count(s.serial) >= ALL (SELECT aantalinterventies2 / COUNT(se.serial)
FROM servers s
LEFT OUTER JOIN
(SELECT
serial,
COUNT(serial) AS aantalinterventies2
FROM interventies) AS interventies USING (serial)) AS interventiesrest