Karthik Karthik - 2 months ago 9
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

1.1.1.1 ipaddress gateway
2.2.2.2 ipaddress_2 firewall
www.google.com domain google

Answer

You can do it more easily with grep:

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

or

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