更换一定数量的数字范围 [英] Replace range of numbers with certain number
问题描述
我需要一定数量的更换范围内的数字。我真的努力很难code将其与自己的sed(像sedS / [33-64] / 64 /)或AWK,但总是得到错误的结果。它往往以取代个位数,而不是数字...我需要将什么:更换0-32 - 它们之间> 255 - > 32,33-64 - > 64,65-128 - > 128,129-255数字IP地址,它应该保持不变。我觉得这个命令选择所有,但IP地址:
sed的'/ [0-9] \\ {1,3 \\} \\。[0-9] \\ {1,3 \\} \\。[0-9] \\ { 1,3 \\} \\。[0-9] \\ {1,3 \\} /! ......
所以我有一个这样的文件:
65.74.16.161
232
10.128.8.72
63
10.128.14.13
100
10.128.8.58
32
10.128.4.129
60
10.128.240.18
59
和它应该是这样的:
65.74.16.161
255
10.128.8.72
64
10.128.14.13
128
10.128.8.58
32
10.128.4.129
64
10.128.240.18
64
的 [33-64]
定义字符类,是写作的一种有趣的方式 [3-6]
键,确实只匹配单个字符 - 如果你真的想与 SED
,而你关心从33到64的值,那么你必须把它不同的写出来 - 而且更冗长
在部分这取决于哪个版本的 SED
你有。将与经典的 SED
工作的解决方案是:
的sed -e'S / ^ [0-9] $ / 32 /'\\
-e的/ ^ [12] [0-9] $ / 32 /'\\
-e'S / ^ 3 [012] $ / 32 /'\\
-e'S / ^ 3 [3-9] $ / 64 /'\\
-e的/ ^ [45] [0-9] $ / 64 /'\\
-e'S / ^ 6 [0-4] $ / 64 /'\\
-e'S / ^ 6 [5-9] $ / 128 /'\\
-e的/ ^ [7-9] [0-9] $ / 128 /'\\
-e'S / ^ 1 [01] [0-9] $ / 128 /'\\
-e'S / ^ 12 [0-8] $ / 128 /'\\
-e'S / ^ $ 129/255 /'\\
-e'S / ^ 1 [3-9] [0-9] $ / 255 /'\\
-e'S / ^ 2 [0-4] [0-9] $ / 255 /'\\
-e'S / ^ 25 [0-5] $ / 255 /'
但是,正如你所看到的,这是很痛苦的。如果你有GNU SED
,您可以使用 -r
选项启用扩展的正前pressions;如果你有Mac OS X或BSD SED
,您可以使用 -E
选项启用扩展的正前pressions。然后你就可以减少上方来的code:
SED-E \\
-e'S / ^([0-9] | [12] [0-9] | 3 [012])$ / 32 /'\\
-e'S / ^(3 [3-9] | [45] [0-9] | 6 [0-4])$ / 64 /'\\
-e'S / ^(6 [5-9] | [7-9] [0-9] | 1 [01] [0-9] | 12 [0-8])$ / 128 /'\\
-e'S / ^(129 | 1 [3-9] [0-9] | 2 [0-4] [0-9] | 25 [0-5])$ / 255 /'
不过,你可能会做的更好使用 AWK
:
的awk'/ ^ [0-9] [0-9] * $ / {如果($ 1 LT = 32)打印32
否则如果($ 1 LT; = 64)打印64
否则如果($ 1 LT = 128)打印128
否则如果($ 1 LT = 255)打印255
否则打印$ 1
下一个
}
{}打印
最后其他
子句准确地打印出任何意外的值,如256或999,或者更确切地说,123456789有那些谁这样写 1
在的{打印}
- 相匹配,并打印出 AWK
脚本的一部分IP地址。
I need to replace a range of numbers with a certain number. I really tried it hard to code it myself with sed (like sed "s/[33-64]/64/") or awk, but always get wrong results. It tends to replace single digits instead of numbers... What I need would be: Replacing 0-32 -> 32, 33-64 -> 64, 65-128 -> 128, 129-255 -> 255. In between these numbers are IPs, which should stay untouched. I think this command is selecting all, but IPs:
sed '/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/! ... '
So I have a file like this:
65.74.16.161
232
10.128.8.72
63
10.128.14.13
100
10.128.8.58
32
10.128.4.129
60
10.128.240.18
59
and it should look like this:
65.74.16.161
255
10.128.8.72
64
10.128.14.13
128
10.128.8.58
32
10.128.4.129
64
10.128.240.18
64
The [33-64]
defines a character class and is a funny way of writing [3-6]
and does indeed only match a single character — any single digit from 3, 4, 5 or 6. If you really want to do it with sed
, and you're concerned with values from 33 to 64, then you have to write it out differently — and much more verbosely.
In part it depends on which version of sed
you have. A solution that will work with classic sed
is:
sed -e 's/^[0-9]$/32/' \
-e 's/^[12][0-9]$/32/' \
-e 's/^3[012]$/32/' \
-e 's/^3[3-9]$/64/' \
-e 's/^[45][0-9]$/64/' \
-e 's/^6[0-4]$/64/' \
-e 's/^6[5-9]$/128/' \
-e 's/^[7-9][0-9]$/128/' \
-e 's/^1[01][0-9]$/128/' \
-e 's/^12[0-8]$/128/' \
-e 's/^129$/255/' \
-e 's/^1[3-9][0-9]$/255/' \
-e 's/^2[0-4][0-9]$/255/' \
-e 's/^25[0-5]$/255/'
But, as you can see, it is quite painful. If you have GNU sed
, you can use the -r
option to enable extended regular expressions; if you have Mac OS X or BSD sed
, you can use the -E
option to enable extended regular expressions. Then you can reduce the code above to:
sed -E \
-e 's/^([0-9]|[12][0-9]|3[012])$/32/' \
-e 's/^(3[3-9]|[45][0-9]|6[0-4])$/64/' \
-e 's/^(6[5-9]|[7-9][0-9]|1[01][0-9]|12[0-8])$/128/' \
-e 's/^(129|1[3-9][0-9]|2[0-4][0-9]|25[0-5])$/255/'
However, you might do better using awk
:
awk '/^[0-9][0-9]*$/ { if ($1 <= 32) print 32
else if ($1 <= 64) print 64
else if ($1 <= 128) print 128
else if ($1 <= 255) print 255
else print $1
next
}
{ print }'
The final else
clause accurately prints any unexpected values, such as 256 or 999 or, indeed, 123456789. There are those who would write 1
in place of { print }
— the part of the awk
script that matches and prints the IP addresses.
这篇关于更换一定数量的数字范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!