テキスト内のすべてのフレーズを検索して別のフレーズに置き換える方法
(by using sed and/or awk and/or grep and/or perl) ?
これは、ある IPv4 アドレスを別のアドレスに置き換え、見つかった値が正しい IPv4 アドレスであることを確認することです。
次に例を示します。
ip_node:<ip_address> e.g. ip_node:192.168.0.1
これらは
ip_address:<ip_address>” e.g. "ip_address:192.168.0.1"
追加の仮定:
-> Phrases such
192.168.000.001, 072.12.01.1, 256.224.1.010, 20.128.300.01
and similar aren't propoer IPv4 addresses and should be marked as
invalid IPv4 address and possibly write to another file.
-> Phrases containing less/more than 4 octets aren't also considered
to be a valid IPv4 addresses and like above should be marked as
invalid IPv4 address and possibly write to another file.
-> Phrases containing any character other than a digit [0..9] aren't also
considered to be a valid IPv4 addresses and like above should be marked as
invalid IPv4 address and possibly write to another file.
サンプルファイルの内容:
ip_node:192.168.0.1
ip_node:192.268.0.01
ip_node:10.0.0.0
ip_node:10.0.0000.10
ip_node:10.255.255.255
ip_node:10.255.255.255.12
ip_node:172.16.0.0
ip_node:172.16.0
ip_node:172.31.255.255
ip_node:172.31.255.
ip_node:0.0.0.0
ip_node:01.0.01.0
ip_node:255.255.255.255
ip_node:255.259.255.259
ip_node:224.0.0.0
ip_node:224.0.
ip_node:207.142.131.236
ip_node:207.002.001.06
ip_node:255.255.255.0
ip_node:055.2255.1255.0
ip_node:204.144.134.234
ip_node:2o7.0o2.0E.O6
ip_node:245.245.245.40
ip_node:O55.2255.1255.a0
出力 file1-all の内容 (すべてのエントリを含む):
ip_address:192.168.0.1
[!]ip_node:192.268.0.01 -- [invalid IP address]
ip_address:10.0.0.0
[!]ip_node:10.0.0000.10 -- [invalid IP address]
ip_address:10.255.255.255
[!]ip_node:10.255.255.255.12 -- [invalid IP address]
ip_address:172.16.0.0
[!]ip_node:172.16.0 -- [invalid IP address]
ip_address:172.31.255.255
[!]ip_node:172.31.255. -- [invalid IP address]
ip_address:0.0.0.0
[!]ip_node:01.0.01.0 -- [invalid IP address]
ip_address:255.255.255.255
[!]ip_node:255.259.255.259 -- [invalid IP address]
ip_address:224.0.0.0
[!]ip_node:224.0. -- [invalid IP address]
ip_address:207.142.131.236
[!]ip_node:207.002.001.06 -- [invalid IP address]
ip_address:255.255.255.0
[!]ip_node:055.2255.1255.0 -- [invalid IP address]
ip_address:204.144.134.234
[!]ip_node:2o7.0o2.0E.O6 -- [invalid IP address]
ip_address:245.245.245.40
[!]ip_node:O55.2255.1255.a0 -- [invalid IP address]
出力 file2-bad の内容 (不正なエントリのみ):
[!]ip_node:192.268.0.01 -- [invalid IP address]
[!]ip_node:10.0.0000.10 -- [invalid IP address]
[!]ip_node:10.255.255.255.12 -- [invalid IP address]
[!]ip_node:172.16.0 -- [invalid IP address]
[!]ip_node:172.31.255. -- [invalid IP address]
[!]ip_node:01.0.01.0 -- [invalid IP address]
[!]ip_node:255.259.255.259 -- [invalid IP address]
[!]ip_node:224.0. -- [invalid IP address]
[!]ip_node:207.002.001.06 -- [invalid IP address]
[!]ip_node:055.2255.1255.0 -- [invalid IP address]
[!]ip_node:2o7.0o2.0E.O6 -- [invalid IP address]
[!]ip_node:O55.2255.1255.a0 -- [invalid IP address]
出力 file3-good の内容 (有効なエントリのみ):
ip_address:192.168.0.1
ip_address:10.0.0.0
ip_address:10.255.255.255
ip_address:172.16.0.0
ip_address:172.31.255.255
ip_address:0.0.0.0
ip_address:255.255.255.255
ip_address:224.0.0.0
ip_address:207.142.131.236
ip_address:255.255.255.0
ip_address:204.144.134.234
ip_address:245.245.245.40
試み:
sed -i -e "s/ip_node:/ip_address:/g" <file>
この:
sed -i -e "s/ip_node:^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/ip_address:^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/g" <file>