要获得对坐标,我需要用逗号捆绑它们。
示例输入(空格分割)
161768.669 467646.462 161716.059 467515.711 161683.343 467434.35 161696.535 467427.091 161713.457 467416.235
161719.332 467412.183 161732.58 467403.047 161763.757 467381.118 161791.136 467367.094 161808.447 467361.612 161834.383 467353.808
161842.354 467351.71 161870.358 467344.337
161886.521 467338.28 161905.239 467330.484 161908.865 467328.974 161972.72 467301.688 161984.372 467296.404 162000.0 467288.568 162056.152 467260.414
162065.771 467254.859 162102.992 467233.363 162138.102 467213.086
162146.165 467209.29 162165.68 467200.102 162165.684 467200.1 162270.285 467154.666 162318.855 467133.104 162358.101 467117.844 162165.004 466820.119 162176.208 466809.875
此尝试仅替换每行上的第一次出现
sed 's/ /,/1' file
161768.669,467646.462 161716.059 467515.711 161683.343 467434.35 161696.535 467427.091 161713.457 467416.235
161719.332,467412.183 161732.58 467403.047 161763.757 467381.118 161791.136 467367.094 161808.447 467361.612 161834.383 467353.808
161842.354,467351.71 161870.358 467344.337
161886.521,467338.28 161905.239 467330.484 161908.865 467328.974 161972.72 467301.688 161984.372 467296.404 162000.0 467288.568 162056.152 467260.414
162065.771,467254.859 162102.992 467233.363 162138.102 467213.086
162146.165,467209.29 162165.68 467200.102 162165.684 467200.1 162270.285 467154.666 162318.855 467133.104 162358.101 467117.844 162165.004 466820.119 162176.208 466809.875
期望的输出
161768.669,467646.462 161716.059,467515.711 161683.343,467434.35 161696.535,467427.091 161713.457,467416.235
161719.332,467412.183 161732.58,467403.047 161763.757,467381.118 161791.136,467367.094 161808.447,467361.612 161834.383,467353.808
161842.354,467351.71 161870.358,467344.337
161886.521,467338.28 161905.239,467330.484 161908.865,467328.974 161972.72,467301.688 161984.372,467296.404 162000.0,467288.568 162056.152,467260.414
162065.771,467254.859 162102.992,467233.363 162138.102,467213.086
162146.165,467209.29 162165.68,467200.102 162165.684,467200.1 162270.285,467154.666 162318.855,467133.104 162358.101,467117.844 162165.004,466820.119 162176.208,466809.875
这可以用sed或其他方式完成吗?
答案 0 :(得分:3)
对于所有出现的事件(标志g
),您想要一个数字后跟一个空格和一个数字替换为数字后跟逗号和数字。
sed 's/\([0-9]\+\.[0-9]\+\) \([0-9]\+\.[0-9]\+\)/\1,\2/g'
你可能想要改变一个"数字"已定义,但只要在小数点后的和之前有数字,这就应该有效,并且你总是有一个小数点。
如果你根本不关心这些是否是数字,你可以做得更简单。
答案 1 :(得分:2)
我会选择awk
,因为您可以轻松地遍历字段:
awk '{for (i=1; i<=NF; i+=2) printf "%s,%s%s", $i, $(i+1), (i==NF-1?"\n":" ")' file
这个想法是以两个为一的块循环遍历字段。因此:
for (i=1; i<=NF; i+=2)
正常循环,种子为2.(将其称为“种子”是否正确?只是猜测)printf "%s,%s%s", $i, $(i+1), (i==NF-1?"\n":" ")
打印以逗号分隔的两个字段。它会根据循环位置添加一个空格或换行符。这也有效:
awk '{for (i=1; i<=NF; i+=2) printf "%s,%s ", $i, $(i+1); print ""}'
$ awk '{for (i=1; i<=NF; i+=2) printf "%s,%s ", $i, $(i+1); print ""}' file
161768.669,467646.462 161716.059,467515.711 161683.343,467434.35 161696.535,467427.091 161713.457,467416.235
161719.332,467412.183 161732.58,467403.047 161763.757,467381.118 161791.136,467367.094 161808.447,467361.612 161834.383,467353.808
161842.354,467351.71 161870.358,467344.337
161886.521,467338.28 161905.239,467330.484 161908.865,467328.974 161972.72,467301.688 161984.372,467296.404 162000.0,467288.568 162056.152,467260.414
162065.771,467254.859 162102.992,467233.363 162138.102,467213.086
162146.165,467209.29 162165.68,467200.102 162165.684,467200.1 162270.285,467154.666 162318.855,467133.104 162358.101,467117.844 162165.004,466820.119 162176.208,466809.875
答案 2 :(得分:0)
红宝石单行:
ruby -ane 'puts $F.each_slice(2).map {|*pair| pair.join ","}.join " "' file
答案 3 :(得分:0)
# posix version (non GNU sed or use --posix)
sed 's/\([^ ]\{1,\}\) \([^ ]\{1,\}\)/\1,\2/g' YourFile
GNU sed version
sed 's/\([^ ]+\) \([^ ]+\)/\1,\2/g' YourFile
用逗号替换非空白组的对等方之间的任何空格。它不会替换2个对等体之间的空间,因为sed在最后一个之后开始下一个替换,所以下一个非空白组不包括以下空格