Karthik Karthik - 8 months ago 54
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


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