From owner-freebsd-questions@FreeBSD.ORG Wed Aug 27 16:38:49 2008 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 812891065689 for ; Wed, 27 Aug 2008 16:38:49 +0000 (UTC) (envelope-from martin@dc.cis.okstate.edu) Received: from dc.cis.okstate.edu (dc.cis.okstate.edu [139.78.103.93]) by mx1.freebsd.org (Postfix) with ESMTP id 5DC028FC1E for ; Wed, 27 Aug 2008 16:38:49 +0000 (UTC) (envelope-from martin@dc.cis.okstate.edu) Received: from dc.cis.okstate.edu (localhost.cis.okstate.edu [127.0.0.1]) by dc.cis.okstate.edu (8.14.2/8.13.8) with ESMTP id m7RGckOP032351; Wed, 27 Aug 2008 11:38:46 -0500 (CDT) (envelope-from martin@dc.cis.okstate.edu) Message-Id: <200808271638.m7RGckOP032351@dc.cis.okstate.edu> To: freebsd-questions@freebsd.org, Paul Chvostek Date: Wed, 27 Aug 2008 11:38:46 -0500 From: Martin McCormick Cc: Subject: Re: Regular Expression Trouble X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 27 Aug 2008 16:38:49 -0000 Paul Chvostek writes: > While I agree with others that awk should be used with explicit > recognition of the particular lines, you can still snatch everything > with sed if you want to. In FreeBSD, sed supported extended regex, so: > > sed -nE 's/.*([0-9a-f]{2}(:[0-9a-f]{2}){5}).*/\1/p' > > The "-n" option tells sed not to print the line unless instructed to > explicitely, and the "p" modifier at the end is that instruction. As > for the regex ... well, that's straightforward enough. > > > This is an attempt to isolate every MAC address that > > appears and then sort and count them to see who is having > > trouble or, in some cases, is causing trouble. > > Then you still may want to use awk for some of that... Actually, I have been using awk, but maybe not as efficiently as I could be, judging from the responses. I was hoping that the sed script would recognize 6 pairs of hex digits connected by :'s no matter where they appeared in a line and give me just that pattern match as, in this case, I don't care why the MAC address printed, only that it did and having nothing but MAC's makes the rest of the sorting and counting trivial. Other helpful examples not quoted but much appreciated. . . > Hope this helps. It helps a lot. Awk is one of those things that one can use for years and still not exploit all the good things it has. I am amazed even after years of using UNIX how much genius is packed in to the basic system. One last sed observation. I did fail to use the -E flag so sed didn't know it should be using extended RE's. I will give your examples a try for both sed and awk and see what new capabilities I can come up with. Again, a thousand thanks to you and everyone else for your answers and patience. It is good to see many different ways of solving the same problem. Martin McCormick