[plug] Splitting data between patterns

Keith Bawden keith at bawdo.com
Sun Jul 20 02:37:53 WST 2008


On Fri, Jul 18, 2008 at 11:51, Nathan Alberti <nalberti at gmail.com> wrote:
> I need to split it out into individual variables, each block separated by
> "#" characters represents a different host.
> I'm sure awk/grep can do what I want just hoping for an example I can work
> with.

I'm assuming you are going to do this within a script of sorts (you
did mention splitting each subset into a variable). For this I
personally would use either perl or ruby.  Awk could probably be
smacked into submission, but I go a bit cross eyed when trying to do
"tricky" things with it. On the other hand if you just want each host
split out to a seperate file then csplit as mentioned by Daniel is the
way to go :)

Anyway the way I would do it would be to:

1. Iterate over each line in the file.
2. Increment a counter when hitting a line with  "######" on it.
3. Catenate all non blank and non "######" lines into an array or hash
using the incremented counter as the key.
4. Then when need be you can access a host with something like $host{12}

Hope this gives you some ideas on how to proceed.

Regards, Keith



More information about the plug mailing list