markgoz has asked for the wisdom of the Perl Monks concerning the following question:

I have been trying to format a file which contains lines like the ones below over and over again.
answer-group mark.example.com owner "example" type vip answer-add 10.0.0.1 name mark1.example.com weight 1 answer-add 10.0.0.2 name mark2.example.com weight 1 answer-add 10.0.0.3 name mark3.example.com weight 1 answer-group jeff.example.com owner "example" type vip answer-add 10.0.0.9 name jeff1.example.com weight 1 answer-add 10.0.0.15 name jeff2.example.com weight 1
I would just like to format this file with a script so that the output looks like as follows:
mark.example.com,10.0.0.1,10.0.0.2,10.0.0.3 jeff.example.com,10.0.0.9,10.0.0.15
I have been testing this out using "while" or "foreach" loops while also using "split" to get the specific IP and/or example.com names, but I cannot get the script to output the data in the same line while reading from a filehandle. Any help here would be appreciated.

Replies are listed 'Best First'.
Re: Format lines in a File to be outputted in a different sequence
by Kenosis (Priest) on Jan 23, 2014 at 19:09 UTC

    Welcome to PerlMonks, markgoz.

    It would be helpful if you'd enclose the 'before' and 'after' datasets within <code> tags, so the wanted formatting is apparent.

Re: Format lines in a File to be outputted in a different sequence
by toolic (Bishop) on Jan 23, 2014 at 19:09 UTC
    • Read Writeup Formatting Tips, then edit your node to add "code" tags around your file. We can't tell where 1 line starts and another ends.
    • Show the output you expect to get.
    • Show the code you have tried.
Re: Format lines in a File to be outputted in a different sequence
by kcott (Archbishop) on Jan 24, 2014 at 12:51 UTC

    G'day markgoz,

    Welcome to the monastery.

    Your post is unreadable with respect to the format of your data (as already pointed out).

    Your question is about formatting so the format of your data is clearly important: despite two requests (within minutes of you posting) to fix this, it would appear you've done nothing about this. Perhaps you need to read "How do I change/delete my post?".

    You also make some vague reference to having tried different solutions: despite being asked to show your code, it appears you've nothing about this either.

    The following may point you in the right direction:

    #!/usr/bin/env perl -l use strict; use warnings; print join ' ' => (split)[1, 0, 3, 2] while <DATA>; __DATA__ a b c d e f g h i j k l

    Output:

    b a d c f e h g j i l k

    If you want further help with this, please make some effort to help us to help you.

    -- Ken

Re: Format lines in a File to be outputted in a different sequence
by Anonymous Monk on Jan 27, 2014 at 10:55 UTC

    Hello markgoz,

    It sounds like you've already got the right approach by reading your input line-by-line with a traditional while loop and doing a split on each line. Then, since you want to collect multiple input lines into one output line, you could collect the input data in a temporary data structure until you have enough to print an output line. Or, if your input and output is always ordered in the way you showed, a simpler (and less flexible) approach would be to only include a \n newline in the output when you want one, instead of for every line of input.

    If you need any help with a specific piece of code, feel free to post that.