ank*_*hen 1 regex linux awk grep
我有一个 IP 文件,每一行都有两个或三个逗号分隔的 IP,例如 -
ip x 171.48.179.64, 194.88.105.83, 10.121.15.191
ip x 122.176.17.76, 194.88.105.83, 10.121.15.191
ip x 223.179.196.169, 10.121.15.135
ip x 157.41.161.64, 10.121.15.135
ip x 49.14.160.119, 10.121.15.191
ip x 157.41.230.108, 10.121.15.191
ip x 101.208.189.88, 194.88.105.83, 10.121.15.191
ip x 180.215.137.150, 194.88.105.83, 10.121.15.191
ip x 157.41.161.64, 10.121.15.191
ip x 157.41.161.64, 10.121.15.191
Run Code Online (Sandbox Code Playgroud)
所以我想 grep 包含两个逗号(三个 IP)的行,例如 -
ip x 171.48.179.64, 194.88.105.83, 10.121.15.191
ip x 122.176.17.76, 194.88.105.83, 10.121.15.191
ip x 101.208.189.88, 194.88.105.83, 10.121.15.191
ip x 180.215.137.150, 194.88.105.83, 10.121.15.191
Run Code Online (Sandbox Code Playgroud)
我搜索了很多,但没有得到任何具体的答案,请帮助我。
您可以使用awk:
awk -F, 'NF==3' file
Run Code Online (Sandbox Code Playgroud)
-F,将分隔符设置为,. NF是一个特殊变量,包含字段数。如果满足条件NF==3awk 将打印当前行。
使用 (e)grep 将是这样的:
grep -E '^([^,]+,){2}[^,]+$' file
Run Code Online (Sandbox Code Playgroud)
我个人会使用 awk,因为它更具可读性。