使用AWK移调 [英] Transpose using AWK

查看:115
本文介绍了使用AWK移调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
使用AWK或Perl进行移置

Possible Duplicate:
Transpose using AWK or Perl

我想使用AWK获取以下格式的输出文件.我的输入文件是一个用空格分隔的文本文件.任何帮助,将不胜感激.谢谢

Hi I would like to use AWK to get an output file in the format below. My input file is a space separated text file. Any help with this would be appreciated. Thanks

输入文件

id  quantity colour shape   size colour shape     size  colour  shape   size
1   10       blue   square  10   red    triangle   12   pink    circle  20
2   12       yellow pentagon 3   orange rectangle   4   purple   oval   6

所需的输出

id  colour  shape    size
1   blue    square   10
1   red     triangle 12
1   pink    circle   20
2   yellow  pentagon  3
2   orange  rectangle 4
2   purple  oval      6

推荐答案

这是通用的,因此您可以按名称选择要输出的列.我已经在输入数据中的其他列上进行了测试,无论它们处于什么位置都不会输出.

This is generalized so you can choose columns for output by name. I've tested it with additional columns in the input data and they don't get output regardless of their position.

#!/usr/bin/awk -f
BEGIN {
    col_list = "colour shape size"
    # Use a B ("blank") to add spaces in the output before or
    # after a format string (e.g. %6dB), but generally use the numeric argument
    col_fmt = "%-12s %-14s %5d"
    # columns to be repeated on multiple lines may appear anywhere in
    # the input, but they will be output together at the beginning of the line
    repeat_fields["id"]
    # since these are individually set we won't use B
    repeat_fmt["id"] = "%4d "
    # additional fields to repeat on each line
    #repeat_fields["another"]
    #repeat_fmt["another"] = "%8s"
    ncols = split(col_list, cols)
    split(col_fmt, fmts)
    for (i = 1; i <= ncols; i++) {
        col_names[cols[i]]
        forms[cols[i]] = fmts[i]
    }
}

# save the positions of the columns using the header line
FNR == 1 {
    for (i = 1; i <= NF; i++) {
        if ($i in repeat_fields) {
            repeat[++nrepeats] = i
            repeat_look[i] = i
            rformats[i] = repeat_fmt[$i]
        }
        if ($i in col_names) {
            col_nums[++n] = i
            col_look[i] = i
            formats[i] = forms[$i]
        }
    }
    # print the header line
    for (i = 1; i <= nrepeats; i++) {
        f = rformats[repeat[i]]
        sub("d", "s", f)
        gsub("B", " ", f)
        printf f, $repeat[i]
    }
    for (i = 1; i <= ncols; i++) {
        f = formats[col_nums[i]]
        sub("d", "s", f)
        gsub("B", " ", f)
        printf f, $col_nums[i]
    }
    printf "\n"
    next
}

{
    for (i = 1; i <= NF; i++) {
        if (i in repeat_look) {
            f = rformats[i]
            gsub("B", " ", f)
            repeat_out = repeat_out sprintf(f, $i)

        }
        if (i in col_look) {
            f = formats[i]
            gsub("B", " ", f)
            out = out sprintf(f, $i)
            coln++
        }
        if (coln == ncols) {
            print repeat_out out
            out = ""
            coln = 0
        }
    }
    repeat_out = ""
}

使用修改后的输入数据:

With this modified input data:

no id  colour base   shape    size colour shape     size  colour  shape   size material
14 1   blue   twenty square   10   red    triangle   12   pink    circle  20   wool
23 2   yellow ninety pentagon 3    orange rectangle   4   purple  oval    6    cotton

输出为:

  id colour      shape          size
   1 blue        square           10
   1 red         triangle         12
   1 pink        circle           20
   2 yellow      pentagon          3
   2 orange      rectangle         4
   2 purple      oval              6

这篇关于使用AWK移调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆