From owner-freebsd-questions Wed Mar 12 3: 9: 3 2003 Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id A7A0E37B405 for ; Wed, 12 Mar 2003 03:09:01 -0800 (PST) Received: from smtp.infracaninophile.co.uk (smtp.infracaninophile.co.uk [81.2.69.218]) by mx1.FreeBSD.org (Postfix) with ESMTP id 01CF843F85 for ; Wed, 12 Mar 2003 03:08:59 -0800 (PST) (envelope-from m.seaman@infracaninophile.co.uk) Received: from happy-idiot-talk.infracaninophile.co.uk (localhost [127.0.0.1]) by smtp.infracaninophile.co.uk (8.12.8/8.12.8) with ESMTP id h2CB8qJ6006019 for ; Wed, 12 Mar 2003 11:08:52 GMT (envelope-from matthew@happy-idiot-talk.infracaninophile.co.uk) Received: (from matthew@localhost) by happy-idiot-talk.infracaninophile.co.uk (8.12.8/8.12.8/Submit) id h2CB8qCH006012 for freebsd-questions@FreeBSD.ORG; Wed, 12 Mar 2003 11:08:52 GMT Date: Wed, 12 Mar 2003 11:08:52 +0000 From: Matthew Seaman To: freebsd-questions@FreeBSD.ORG Subject: global regular expression print Message-ID: <20030312110852.GB47126@happy-idiot-talk.infracaninophi> Mail-Followup-To: Matthew Seaman , freebsd-questions@FreeBSD.ORG References: <22358.1047465509@www63.gmx.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <22358.1047465509@www63.gmx.net> User-Agent: Mutt/1.5.3i X-Spam-Status: No, hits=-32.5 required=5.0 tests=EMAIL_ATTRIBUTION,IN_REP_TO,QUOTED_EMAIL_TEXT,REFERENCES, REPLY_WITH_QUOTES,USER_AGENT_MUTT version=2.50 X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp) Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG On Wed, Mar 12, 2003 at 11:38:29AM +0100, Pascal Giannakakis wrote: > Hello, > > i want to extract a word with a regular expressions from a line of text. > Here is what > i currently have: > > grep -c -E 'Interesting ports on [a-zA-Z0-9-.]+ \((.+)\)' > > This will print lines (from nmap), but i need only the 1st match in > parentheses, which > is the IP in this case. What programm would i use to print what grep sees as > \1, > instead of the whole line? There are any number of solutions to this. Here are two possibilities. Assuming that lines of interest in the file always start with 'Interesting ports...' awk '/^Interesting ports on [a-zA-Z0-9-.]+/{ print $5; }' filename Or more generally: perl -ne 'm/Interesting ports on [a-zA-Z0-9-.]+ \((.+)\)/ && print "$1\n";' filename Cheers, Matthew -- Dr Matthew J Seaman MA, D.Phil. 26 The Paddocks Savill Way PGP: http://www.infracaninophile.co.uk/pgpkey Marlow Tel: +44 1628 476614 Bucks., SL7 1TH UK To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message