1

我有一个看起来像这样的输出,其中第一个数字对应于下面类型的计数(例如 72 表示类型 4 等)

 72
 Type
 4
 51
 Type
 5
 66
 Type
 6
 78
 Type
 7
 ..etc

有没有办法组织这些数据看起来像这样:

 Type 4 = 72 times
 Type 5 = 51 times
 Type 6 = 66 times 
 etc.. 

本质上,问题是如何获取单列数据并使用 bash、awk、python 等将其排序/组织成更具可读性的内容(理想情况下,在 bash 中,但有兴趣知道如何在 Python 中执行)。

谢谢你。

4

4 回答 4

4

用于paste从标准输入连接 3 个连续行,然后重新排列字段。

paste - - - < file | awk '{print $2, $3, "=", $1, "times"}'
于 2013-09-02T19:59:00.797 回答
2

使用 Python 一次读取三行数据非常简单:

def perthree(iterable):
    return zip(*[iter(iterable)] * 3)

with open(inputfile) as infile:
    for count, type_, type_num in perthree(infile):
        print('{} {} = {} times'.format(type_.strip(), type_num.strip(), count.strip()))

这些.strip()调用会删除任何额外的空格,包括每行输入文本末尾的换行符。

演示:

>>> with open(inputfile) as infile:
...     for count, type_, type_num in perthree(infile):
...         print('{} {} = {} times'.format(type_.strip(), type_num.strip(), count.strip()))
... 
Type 4 = 72 times
Type 5 = 51 times
Type 6 = 66 times
Type 7 = 78 times
于 2013-09-02T19:32:21.557 回答
2

试试这个 awk 一个班轮:

$ awk 'NR%3==1{n=$1}NR%3==2{t=$1}NR%3==0{print t,$1,"=",n,"times"}' file
Type 4 = 72 times
Type 5 = 51 times
Type 6 = 66 times
Type 7 = 78 times

这个怎么运作?

awk '
    NR%3==1{ # if we are on lines 1,4,7, etc (NR is the record number (or the line number)
        n=$1 # set the variable n to the first (and only) word
    }
    NR%3==2{ # if we are on lines 2,5,7, etc 
        t=$1 # set the variable t to the first (and only) word
    }
    NR%3==0{ # if we are on lines 3,6,9, etc
        print t,$1,"=",n,"times" # print the desired output 
    }' file
于 2013-09-02T19:32:25.377 回答
2

在 Bash 中:

#!/bin/bash
A=() I=0
while read -r LINE; do
    if (( (M = ++I % 3) )); then
        A[M]=$LINE
    else
        printf "%s %s = %s times\n" "${A[2]}" "$LINE" "${A[1]}"
    fi
done

运行bash script.sh < file创建:

Type 4 = 72 times
Type 5 = 51 times
Type 6 = 66 times
Type 7 = 78 times

注意:使用默认 IFS ( $' \t\n'),默认情况下read会删除前导和尾随空格。

于 2013-09-02T19:35:49.370 回答