Date: Fri, 18 Jan 2008 13:34:49 -0500 From: Randy Pratt <bsd-unix@embarqmail.com> To: Paul Schmehl <pauls@utdallas.edu> Cc: FreeBSD Questions <freebsd-questions@freebsd.org> Subject: Re: Shell scripting kungfu Message-ID: <20080118133449.8c6972b8.bsd-unix@embarqmail.com> In-Reply-To: <043D043618599C4017DE8774@utd59514.utdallas.edu> References: <043D043618599C4017DE8774@utd59514.utdallas.edu>
next in thread | previous in thread | raw e-mail | index | archive | help
On Fri, 18 Jan 2008 11:09:14 -0600
Paul Schmehl <pauls@utdallas.edu> 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
--
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20080118133449.8c6972b8.bsd-unix>
