#!/usr/bin/perl $\ = $/; open(F, "test.txt") or die"$!";; while(<F>) { print $1 if(/.*?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*/); } close F;