Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you extract IP addresses from files using a regex in a linux shell?

How to extract a text part by regexp in linux shell? Lets say, I have a file where in every line is an IP address, but on a different position. What is the simplest way to extract those IP addresses using common unix command-line tools?

like image 312
Kazimieras Aliulis Avatar asked Jan 09 '09 13:01

Kazimieras Aliulis


People also ask

How do you find IP address in a file in Linux?

In Linux you can use regular expressions with grep to extract an IP address from a file. The grep command has the -E (extended regex) option to allow it to interpret a pattern as a extended regular expression.

Which command is used to extract IP address from local machine Linux?

Get your Private IP Address with the ifconfig Command One way is to use the ifconfig command. ifconfig is a command line program that configures network interfaces on Linux.

How grep all IP address in Linux?

so you can use grep -oE "\b([0-9]{1,3}\.){ 3}[0-9]{1,3}\b" to grep the ip addresses from your output. Thanks. This works.


1 Answers

You could use grep to pull them out.

grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' file.txt 
like image 100
brien Avatar answered Sep 18 '22 20:09

brien