Karthik Karthik - 1 year ago 123
Bash Question

Awk print lines starting with regex (IP address)

Im trying to read a file for those lines which has IP address on the first column.

my command below does not return any value.

cat test.csv | awk '$1 == "^[[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}]" { print $0 }'

The regex can capture IP address.

Tried the below too,

cat test_1.csv | awk '$1~/^[[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\]/ {print $0}'

test.csv ipaddress gateway ipaddress_2 firewall
www.google.com domain google

Answer Source

You can do it more easily with grep:

grep -P '^\d+(\.\d+){3}\s' test.csv


grep -P '^\d{1,3}(\.\d{1,3}){3}\s' test.csv
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download