将纯文本信息转换为 csv 或 excel

将纯文本信息转换为 csv 或 excel

我有包含类似数据的文件,我试图以类似的方式排列这些数据,下面是详细信息。我根据上下文中的变量修改了脚本,无法获得所需的结果。可能是由于我缺乏对 awk 的了解以及对脚本编写的了解有限。

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

我正在尝试以下面的格式获取它。这里我们有两个条件。 M = 物理磁盘数量 N = V磁盘数量 如果 M > N,Virtual_Machine 的行数 = M 如果 M < N,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

请指教。

问候, 达山

答案1

您可以使用 awk 来完成此操作,方法是维护书名和年份以及论文的数组。在您的示例中,论文没有年份,因此它们只是作为标题列在第二列中。

这是一个例子:

#!/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();
}

与输出:

$ ./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

相关内容