【问题标题】:Transpose using AWK or Perl使用 AWK 或 Perl 转置
【发布时间】:2012-07-25 22:25:19
【问题描述】:

您好,我想使用 AWK 或 Perl 来获取以下格式的输出文件。我的输入文件是一个空格分隔的文本文件。这类似于我之前的问题,但在这种情况下,输入和输出没有格式。我的列位置可能会改变,所以会欣赏不引用列号的技术

输入文件

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

我正在使用 Dennis Williamson 的代码。唯一的问题是我得到的输出在转置字段中没有空间分隔。我需要一个空格分隔

#!/usr/bin/awk -f
BEGIN {
col_list = "quantity colour shape"
# 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

# 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"] = "%-1s "
# additional fields to repeat on each line

ncols = split(col_list, cols)

for (i = 1; i <= ncols; i++) {
    col_names[cols[i]]
    forms[cols[i]] = "%-1s"
}
}


# 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 = ""
}

输出

id quantitycolourshape
1 10bluesquare
1 redtrianglepink
2 circle12yellow
2 pentagonorangerectangle

对于之前没有包含有关实际文件的所有信息,我深表歉意。我这样做只是为了简单,但它并没有满足我的所有要求。

在我的实际文件中,我希望为 NODE SITE CHILD 转置字段 n_cell 和 n_bsc

NODE SITE CHILD n_cell n_bsc

Here is a link to the actual file I am working on

【问题讨论】:

  • 语言名称是“Perl”,而不是“PERL”。
  • 然而它是“AWK”。我对这个问题的回答与my answer to your previous question 相同。
  • Transpose using AWK 的可能副本
  • 我已将前一个标记为删除
  • 您的真实文件是什么样的?您不喜欢这种特定格式的解决方案,并且您逐滴传递信息,就像有超过 5,000 列一样。前两列是否总是idquantity?其余列是否始终为colourshapesize?信息总是以三列为一组吗?还有什么我们应该知道的吗?

标签: perl unix awk


【解决方案1】:
<>;
print("id colour shape size\n");

while (<>) {
   my @combined_fields = split;
   my $id = shift(@combined_fields);
   while (@combined_fields) {
       my @fields = ( $id, splice(@combined_fields, 0, 3) );
       print(join(' ', @fields), "\n");
   }
}

【讨论】:

  • perl script.pl infile &gt;outfile 或就地:perl -i script.pl file
  • 我的实际输入文件有超过 5k 列,所以想引用固定列和列以使用标题行而不是列 ID 进行转置
  • @Santosh Pillai,它们总是按相同的顺序排列吗?如果是这样,知道列数会更有用。然后只需将3 更改为我的代码中的那个数字。如果它们的顺序并不总是相同,你怎么知道哪一列属于哪一行?
【解决方案2】:

您告诉我们,您的真实数据包含超过 5,000 列,并且其列位置可能会发生变化,恐怕这还不够。

因此,在没有任何适当信息的情况下,我已经写了这个,它使用标题行来计算数据集的数量和大小,id 列在哪里,以及第一组从哪一列开始。

它适用于您的示例数据,但我只能猜测它是否适用于您的实时文件。

use strict;
use warnings;

my @headers = split ' ', <>;

my %headers;
$headers{$_}++ for @headers;

die "Expected exactly one 'id' column" unless $headers{id} // 0 == 1;
my $id_index = 0;
$id_index++ while $headers[$id_index] ne 'id';

my @labels = grep $headers{$_} > 1, keys %headers;
my $set_size = @labels;
my $num_sets = $headers{$labels[0]};

my $start_index = 0;
$start_index++ while $headers[$start_index] ne $labels[0];

my @reformat;

while (<>) {
  my @fields = split;
  next unless @fields;
  my $id = $fields[$id_index];
  for (my $i = $start_index; $i < @fields; $i+=$set_size) {
    push @reformat, [ $id, @fields[$i..$i + $set_size - 1] ];
  }
}

unshift @labels, 'id';
print "@labels\n";
print "@$_\n" for @reformat;

输出

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-27
    • 2011-06-16
    • 2015-05-05
    • 2011-08-04
    • 2014-03-19
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多