在文件中找到每行的第二个ip地址

时间:2015-06-19 04:32:28

标签: bash

如何找到每行的第二个ip地址?

输入

hostname1,10.160.226.49,10.160.35.80,10.14.1.80,10.14.5.80,10.160.0.27, 2048
hostname2,10.160.235.89,10.160.35.81,10.14.1.81,10.14.5.81,10.157.233.144, 1024
hostname3,10.160.231.247,10.160.35.82,10.14.1.82,10.14.5.82,10.157.239.26, 512
hostname4,10.160.227.232,10.160.35.84,10.14.1.84,10.14.5.84,10.241.14.2, 2048
hostname5,10.160.224.218,10.160.35.85,10.14.1.85,10.14.5.85,10.157.234.82, 1024

输出

10.160.35.80
10.160.35.81
10.160.35.82
10.160.35.83
10.160.35.84
10.160.35.85

1 个答案:

答案 0 :(得分:3)

试试这个:

cut -d "," -f 3 file

或使用GNU grep:

grep -oP '^[^,]*,[^,]*,\K[^,]*' file

或与awk:

awk -F "," '{print $3}' file

或使用bash的内置命令:

while IFS="," read -r a b c d; do echo "$c"; done < file

或使用数组:

while IFS="," read -ra a; do echo "${a[2]}"; done < file

输出:

10.160.35.80
10.160.35.81
10.160.35.82
10.160.35.84
10.160.35.85