从文件中删除特定的字符串

初学者

output.txt

1.1.1.1:22/ does not support password authentication. [ERROR] target ssh://2.2.2.2:22/ does not support password authentication. [ERROR] target ssh://3.3.3.3

我想删除字符串:22/ does not support password authentication. [ERROR] target ssh://output.txt,并把IP地址在新文件

所需的输出:

1.1.1.1
2.2.2.2
3.3.3.3

我尝试过

cat output.txt | grep -vE "(:22/ does not support password authentication. [ERROR] target ssh://)

cat output.txt | egrep -v ":22/ does not support password authentication. [ERROR] target ssh://"

cat output.txt | grep -v ":22/ does not support password authentication. [ERROR] target ssh://"而上述3个命令不删除什么。尝试使用awk-相同的结果:

 awk '{gsub(":22/ does not support password authentication. [ERROR] target ssh://","");print}' output.txt

我没有尝试,sed因为我的字符串包含转义字符

这是完成此工作的perl一线客:

perl -ape 's/.*?(\d+(?:\.\d+){3})/$1\n/g' file.txt 
1.1.1.1
2.2.2.2
3.3.3.3

解释:

s/              # substitute    
    .*?             # 0 or more any character but newline
    (               # start group 1
        \d+         # 1 or more digits
        (?:         # start non capture group
            \.      # a dot
            \d+     # 1 or more digits
        ){3}        # end group, must appear 3 times
    )               # end group 1
/               # with
    $1\n            # content of group 1 (i.e. the IP), followed by linefeed
/g              # global

如果只想匹配IP地址,则必须\d+将上述正则表达式中所有出现的内容替换为:

(?:25[0-5]|2[0-4]\d|[01]\d?\d?)

给出:

s/.*?((?:25[0-5]|2[0-4]\d|[01]\d?\d?)(?:\.(?:25[0-5]|2[0-4]\d|[01]\d?\d?)){3})/$1\n/g    

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章