bash - Find the 2nd ip address per line on file -


how can find 2nd ip address per line?

input

 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 

output

 10.160.35.80 10.160.35.81 10.160.35.82 10.160.35.83 10.160.35.84 10.160.35.85 

try this:

cut -d "," -f 3 file 

or gnu grep:

grep -op '^[^,]*,[^,]*,\k[^,]*' file 

or awk:

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

or bash's builtin commands:

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

or array:

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

output:

 10.160.35.80 10.160.35.81 10.160.35.82 10.160.35.84 10.160.35.85 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -