From owner-freebsd-questions@FreeBSD.ORG Fri Jan 18 18:34:52 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 7589016A417 for ; Fri, 18 Jan 2008 18:34:52 +0000 (UTC) (envelope-from bsd-unix@embarqmail.com) Received: from mailrelay.embarq.synacor.com (mailrelay.embarq.synacor.com [208.47.184.3]) by mx1.freebsd.org (Postfix) with ESMTP id 235F513C447 for ; Fri, 18 Jan 2008 18:34:52 +0000 (UTC) (envelope-from bsd-unix@embarqmail.com) X_CMAE_Category: 0,0 Undefined,Undefined X-CNFS-Analysis: v=1.0 c=1 a=msmEVWto3Ff5DUbb20cA:9 a=VXDBxgiI5dxMX8bhZFsA:7 a=s5-H0bo-mtW2iYCzp81oYy6HheMA:4 a=LY0hPdMaydYA:10 X-CM-Score: 0 X-Scanned-by: Cloudmark Authority Engine Authentication-Results: smtp09.embarq.synacor.com smtp.mail=bsd-unix@embarqmail.com; spf=neutral Authentication-Results: smtp09.embarq.synacor.com smtp.user=rpratt1950@embarqmail.com; auth=pass (LOGIN) Received-SPF: neutral (smtp09.embarq.synacor.com: 76.6.199.167 is neither permitted nor denied by domain of embarqmail.com) Received: from [76.6.199.167] ([76.6.199.167:53788] helo=kt.weeeble.com) by mailrelay.embarq.synacor.com (envelope-from ) (ecelerity 2.2.1.21 r(19176)) with ESMTPA id 11/81-21205-A41F0974; Fri, 18 Jan 2008 13:34:51 -0500 Date: Fri, 18 Jan 2008 13:34:49 -0500 From: Randy Pratt To: Paul Schmehl Message-Id: <20080118133449.8c6972b8.bsd-unix@embarqmail.com> In-Reply-To: <043D043618599C4017DE8774@utd59514.utdallas.edu> References: <043D043618599C4017DE8774@utd59514.utdallas.edu> X-Mailer: Sylpheed 2.4.8 (GTK+ 2.12.5; i386-portbld-freebsd6.3) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Cc: FreeBSD Questions Subject: Re: Shell scripting kungfu 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: Fri, 18 Jan 2008 18:34:52 -0000 On Fri, 18 Jan 2008 11:09:14 -0600 Paul Schmehl wrote: > I need to do the following: > > Take a list of various strings, one of which is a quoted IP address, and > extract the IPs. (Done that.) > > Then take the list of IPs and convert them to a list of IPs with masks on a > single line. > > IOW, I have converted the original list to this: > > x.x.x.x > x.x.x.x > x.x.x.x > x.x.x.x > > Now I need to remove the newlines and add /32, to the end of each IP so that I > have this: > x.x.x.x/32,x.x.x.x/32,x.x.x.x/32,etc. > > I got close with sed, but I'm not quite there. > > I got this: > > x.x.x.x/32,x.x.x.x > x.x.x.x/32,x.x.x.x > x.x.x.x/32,x.x.x.x > > Here's the code I used: > cat hostlist | cut -d',' -f2 | cut -d'"' -f2 | sort | uniq | grep -v "inet" | > sed '/[^*]$/N;s/\n */\/32,/' > > What am I missing? I'm sure you'll get a lot of comments on this one ;-) Here's my one-liner take: while read line; do echo -n "${line}/32,"; done < hostlist | sed 's/\,$//' or to be a bit more understandable: while read line; do echo -n "${line}/32," done < hostlist | sed 's/\,$//' the little sed part just removes the last comma from the list. HTH, Randy --