Home > Blockchain >  Grep IP Range Bash Script
Grep IP Range Bash Script

Time:04-07

I just got into Bash scripting and I'm having a hard time grepping a specific IP range from a file.

I've tried the following:

grep "213.184.111.[128][254]" example.txt

Is there an easier way using seq or sort?

CodePudding user response:

My colleague just helped me out with the following:

grep -P "(213\.184\.111\.(12[8-9]|1[3-9][0-9]|2[0-9][0-9]))"

Is there an easier option?

CodePudding user response:

When awk is also possible. Use . as field separator and compare fourth field.

awk -v start=128 -v end=254 -v FS=. '$4>=start && $4<=end' example.txt

Output:

213.184.111.128
213.184.111.129
213.184.111.130
...
213.184.111.252
213.184.111.253
213.184.111.254

See: 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR

  • Related