1

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
Karthik
  • 363
  • 2
  • 7
  • 20
  • 3
    Use regex operator `~` for matching regex. Not `==`. Also, `awk` can read input from file. You do not need use of `cat`. Simply `awk '$1 ~ /^regex/' file` – sat Sep 03 '16 at 11:29
  • 3
    Post a minimal example of input and output. – sat Sep 03 '16 at 11:32
  • 3
    Why are you putting `[...]` around the regex? That probably doesn't do what you're hoping for here. – Eric Renouf Sep 03 '16 at 11:43
  • @sat is correct about `==` and @EricRenouf is correct about `[...]` and you're also missing a final `}`. You should obviously, have tried a briefer regexp and then built up to that long one. – Ed Morton Sep 03 '16 at 15:09

2 Answers2

4

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
redneb
  • 21,794
  • 6
  • 42
  • 54
0

When you use {1,3} (Interval expressions) in GNU awk, you have to use either --re-interval (or) --posix option to enable it.

Use :

awk --posix '$1 ~ /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/' file

(OR)

awk --re-interval '$1 ~ /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/' file

From man awk:

r{n,m}

One or two numbers inside braces denote an interval expression. Interval expressions are only available if either --posix or --re-interval is specified on the command line.

sat
  • 14,589
  • 7
  • 46
  • 65
  • is there some setting that enables one or both of these options? works fine without either option for me on `GNU Awk 4.1.3`... – Sundeep Sep 03 '16 at 12:19
  • @spasic, It is not working on `GNU Awk 3.1.8`. On `GNU Awk 4` it might be enabled. – sat Sep 03 '16 at 12:24
  • oh ok.. does this work `awk '/^[0-9]{1,3}(\.[0-9]{1,3}){3}\s/' file` with/without those options on `GNU Awk 3.1.8` ? – Sundeep Sep 03 '16 at 12:27
  • 1
    @spasic, It will work with those options only on `GNU Awk 3.1.8`. – sat Sep 03 '16 at 14:38
  • RE intervals are enabled by default since gawk 4.0. On older gawks use `--re-interval`, not `--posix`, to enable RE intervals as the latter additionally disables all gawk extensions, e.g. `gensub()`. – Ed Morton Sep 03 '16 at 15:05
  • 1
    btw your regexp is missing the ending `$` and could be abbreviated to `$1 ~ /^[0-9]{1,3}(\.[0-9]{1,3}){3}$/` – Ed Morton Sep 03 '16 at 15:12