1

I have data output in the below format. I get this output from a command on console.

Number: 9
state: Online
Data:             W1F-9YN
Device: 41
Number: 10
state: Online
Inquiry Data:             W1-9YN                   
Device: 41
Number: 11
state: Online
Inquiry Data:             W1-9YN                   
Device: N/A
Number: 42
state: Online
Data:      WD-W WDZ-04J                

     But, now I want to change it to output in a table format. Like as shown below

Device   number  state    data
41        10     online   WY1-996
42        12     offline  WY2-996
.          .       .        .
.          .       .        .
.          .       .        .

I tried doing with code given below, but I am not able to arrange in right format and some time all data shows up in a single column. Could anyone help me out ?

open WDLIST, "Command";

while (<WDLIST>) {

    if (m/Device\s*:\s*(\d+)/) {

        $enDevice = $1;
        print "$enDevice";
    }

    if (m/Number\s*:\s*(\d+)/) {

        $umber = $1;
        print "$Number";
        chomp;
    }

    if (m/state\s*:\s*(w+)/) {

        $State = $1;
        print"$State";
    }

    if (m/Data\s*:\s*(w+)(d+)(\-)(\s)/) {

        $Data = $1;
        print"$Data";
    }
}

Thank You!

3
  • This is a lot like stackoverflow.com/questions/12606045/… Commented Sep 26, 2012 at 19:03
  • Some tips: 1. You are not matching the last line correctly. Make sure that your regexs match what you want to catch. 2. $umber - is that a typo? 3. You are not printing tabs and you are not printing a newline. Commented Sep 26, 2012 at 19:05
  • That question seems to only have one row of data per input file and for simple data CSV files are quite a bit easier to format. (Until you realize that your data can contain commas and quote chars) Commented Sep 26, 2012 at 19:24

1 Answer 1

1

You can use printf to format your output. The simplest solution would be to replace all your print statements with

printf "%-10s", $variable;

This would print the variable left justified in a 10 character wide column. In addition you need to print a newline either at the start or at the end of each block of data.

For a more complete solution I would gather all data in a hash for a row and print it whenever you detect the end of a block of data:

printf "%-10s %-10s %-10s %-10s\n", $info{device}, $info{number}, $info{state}, $info{data};

(or using a hash slice for less verbose code)

Based on the assumption that each Device field signifies the start of a new device. I would change you code to work like this:

open WDLIST, "Command";

my %device;

printf "%-10s %-10s %-10s %-10s\n", qw(Device number state data);
while (<WDLIST>) {
    if (m/Device\s*:\s*(\d+)/) {

        # Print previous device, if any.
        printf "%-10s %-10s %-10s %-10s\n", @data{ qw(id number state data) }
            if exists $device{id};

        # Reset the current device and set the id
        %device = ( id => $1 );
    }

    if (m/Number\s*:\s*(\d+)/) {
        $device{number} = $1;
    }

    if (m/state\s*:\s*(w+)/) {
        $device{state} = $1;
    }

    if (m/Data\s*:\s*(w+d+-\d+)/) {
        $device{data} = $1;
    }
}
# Print the last device (if any)
printf "%-10s %-10s %-10s %-10s\n", @data{ qw(id number state data) }
    if exists $device{id};

(I'm a bit unsure what the last regexp for the Data field really ought to be. Your example is not consistent. At least you would need to explain the relation between the data fields of your example input and in your example output)

Sign up to request clarification or add additional context in comments.

3 Comments

If I print newline at the end of block meaning if block or while block ? If I put a new line at the end of each if loop it is just giving new line. How do you gather data for a row. You might see that first three should come in first row and next 3 in next row ? I am also little confused on to why it is all printing in single column ? If I give tab also it is not working.
No in both instances I referred to block of data. You need a way to decide which fields belong together. Based on you example it could be that each device starts with a Device: field.
Thanks Mak. But Where are you printing those data. I tried the idea posted by you but it is not printing anything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.