1

テキスト内のすべてのフレーズを検索して別のフレーズに置き換える方法

(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>
4

2 に答える 2

2

「01.0.01.0」または「207.002.001.06」の何が問題になっていますか? なぜ無効だと思いますか?

perl -MRegexp::Common -nle '
    if (/ip_node:\K(\S+)/ && $1 =~ /^($RE{net}{IPv4})$/) {
        print "ip_address", $1;
    } else {
        print "[!]", $_;
    }
' filename 

出力

ip_address192.168.0.1
[!]ip_node:192.268.0.01
ip_address10.0.0.0
[!]ip_node:10.0.0000.10
ip_address10.255.255.255
[!]ip_node:10.255.255.255.12
ip_address172.16.0.0
[!]ip_node:172.16.0
ip_address172.31.255.255
[!]ip_node:172.31.255.
ip_address0.0.0.0
ip_address01.0.01.0
ip_address255.255.255.255
[!]ip_node:255.259.255.259
ip_address224.0.0.0
[!]ip_node:224.0.
ip_address207.142.131.236
ip_address207.002.001.06
ip_address255.255.255.0
[!]ip_node:055.2255.1255.0
ip_address204.144.134.234
[!]ip_node:2o7.0o2.0E.O6
ip_address245.245.245.40
[!]ip_node:O55.2255.1255.a0

目的の出力ファイルに分割するには、次のようにします。

perl -MRegexp::Common -nle '... as above ...' filename |
tee file1-all |
awk '/^\[!\]/ {print > "file2-bad"; next} {print > "file3-good"}'
于 2013-11-06T15:47:11.440 に答える