0

I have file containing similar data, which i am trying to arrange in the similar way, below are the details. I modified the script as per the variables in the context unable to get the required result. Probably it is due to lack of my knowledge on awk and limited knowledge on scripting.

Virtual_Machine  OL6U6  
Vdisk  0004fb00001200005e2ca2d2c7fc2d6f.img size 46GB
Vdisk  0004fb0000120000597ab28f2b6493f8.img size 51GB
Vdisk  0004fb00001200003edc9a2ae9cd5aa6.img size 31GB
Physical_Disk  IBM (796)
device /dev/mapper/dm-0
shareddisk true
Physical_Disk  IBM (829)
device /dev/mapper/dm-1
shareddisk true
Physical_Disk  IBM (830)
device /dev/mapper/dm-2
shareddisk true
Physical_Disk  IBM (742)
device /dev/mapper/dm-3
shareddisk true

I am trying go get it in the below format. Here we have two conditions. M = Number of Physical_Disk N = Number of Vdisk If M > N, rows for Virtual_Machine = M If M < N rows for Virtual_Machine = N

Virtual Machine      Vdisk                                     size           Physical_Disk    device              shareddisk
OL6U6               0004fb00001200005e2ca2d2c7fc2d6f.img       46GB           IBM (796)        /dev/mapper/dm-0     true
OL6U6               0004fb0000120000597ab28f2b6493f8.img       51GB           IBM (829)        /dev/mapper/dm-1     true
OL6U6               0004fb00001200003edc9a2ae9cd5aa6.img       31GB           IBM (830)        /dev/mapper/dm-2     true
OL6U6               -                                         -              IBM (742)         /dev/mapper/dm-2     true

Please advise.

Regards, Darshan

2
  • and what have you tried? which part do you need help with?
    – Jeff Schaller
    Jan 31, 2016 at 13:19
  • I had similar requirement in past where some experts of this forum has provided suggestion and it worked. However in this case i see there is a first column in which some rows are repeating. But corresponding rows in the second column are not.
    – IgniteLX
    Jan 31, 2016 at 13:36

1 Answer 1

1

You can do this with awk, by maintaining arrays for the book title and year, as well as for the papers. In your example, there are no years for the papers, so those are simply listed in the second column as title.

Here is an example:

#!/usr/bin/awk -f
function finish() {
    rows = book;
    if (rows < paper) rows = paper;
    for (n = 0; n <= rows; ++n) {
            printf "%-15s %-25s %-8s %s\n",
            author,
            n <= book ? books[n] : "-",
            n <= book ? years[n] : "-",
            n <= paper ? papers[n] : "-";
    }
    book = -1;
    paper = -1;
}
BEGIN {
    author = "?";
    book = -1;
    paper = -1;
    printf "Author          Books                     year     Papers\n";
}
/^[[:space:]]*Author[[:space:]]/ {
    finish();
    author = $0;
    sub("^[^[:space:]]+[[:space:]]+", "", author);
    sub("[[:space:]]+$", "", author);
    next;
}
/^[[:space:]]*(e)?paper[[:space:]]/ {
    ++paper;
    item = $0;
    sub("^[^[:space:]]+[[:space:]]+", "", item);
    sub("[[:space:]]+$", "", item);
    papers[paper] = item;
    next;
}
/^[[:space:]]*([eE])?[bB]ook[[:space:]].*year[[:space:]]+[[:digit:]]+[[:space:]]*$/ {
    ++book;
    item = $0;
    sub("^[^[:space:]]*[[:space:]]*", "", item);
    sub("[[:space:]]+$", "", item);
    title = item;
    sub("[[:space:]]*year[[:space:]]+[[:digit:]]+$", "", title);
    year = item;
    sub("^.*year[[:space:]]+", "", year);
    books[book] = title;
    years[book] = year;
    next;
}
END {
    finish();
}

with output:

$ ./foo <foo.in
Author          Books                     year     Papers
E. Narayanan    Astrophysics              2001     Intelligent Transportation
E. Narayanan    General Mechanics         2010     Nanotechnology Magazine
E. Narayanan    Nuclear physics           2011     -
R Ramesh        Organic Chemistry         2007     Ionic Batteries
R Ramesh        Physical chemistry        2008     solar photocatalytic oxidation processes
R Ramesh        -                         -        Biological oxidation
3
  • Thomas, i have edited the original post and made some changes, could you please review and help me with respect to it.
    – IgniteLX
    Feb 2, 2016 at 13:36
  • I modified i modified script as per my requirement but however two columns are not populated with required data. I find it quite difficult to post the modified script and output. Could you plz let me know how can i post the details here?
    – IgniteLX
    Feb 2, 2016 at 17:46
  • I have the modified script, input file and output are available at : pasted.co/6313279d
    – IgniteLX
    Feb 8, 2016 at 5:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .