5

假设我有一个 CSV 文件,其标题格式如下:

Field1,Field2
3,262000
4,449000
5,650000
6,853000
7,1061000
8,1263000
9,1473000
10,1683000
11,1893000

我想编写一个 awk 脚本,该脚本将采用逗号分隔的字段名称列表target,将其拆分为一个数组,然后仅挑选出具有我指定名称的那些列。

这是我到目前为止所尝试的,并且我已经验证该head数组包含所需的标头,并且该targets数组包含给定命令行传入的所需目标。

BEGIN{
    FS=","
    split(target, targets, ",")

}

NR==1 {
    for (i = 1; i <= NF; i++) head[i] = $i
}

NR !=1{
    for (i = 1; i <= NF; i++) {
        if (head[i] in targets){
            print $i
        }
    }
}

当我使用命令调用此脚本时

awk -v target=Field1 -f GetCol.awk Debug.csv

我什么也没打印出来。

4

3 回答 3

10

我想通了并发布了答案,以防其他人遇到同样的问题。

它与in我用于测试数组成员资格的关键字有关。此关键字仅测试左侧的操作数是否是右侧数组中的索引之一,而不是值之一。解决方法是创建一个反向查找数组,如下所示。

BEGIN{
    OFS=FS=","
    split(target, t_targets, ",")
    for (i in t_targets)
        targets[t_targets[i]] = i
}
于 2013-04-18T20:25:48.437 回答
6

我的两分钱:

BEGIN{
    OFS=FS=","
    split(target,fields,FS)            # We just set FS don't hard the comma here
    for (i in fields)                  # Distinct var name to aviod headaches
        field_idx[fields[i]] = i       # Reverse lookup 
}
NR==1 {                                # Process header
    for (i=1;i<=NF;i++)                # For each field header
        head[i] = $i                   # Add to hash for comparision with target
    next                               # Skip to next line
}
{                                      # Don't need invert condition (used next)
    sep=""                             # Set for leading separator
    for (i=1;i<=NF;i++)                # For each field
        if (head[i] in field_idx) {    # Test for current field is a target field
            printf "%s%s",sep,$i       # Print the column if matched 
            sep=OFS                    # Set separator to OFS                  
    }
    printf "\n"                        # Print newline character
}
于 2013-04-18T20:41:13.767 回答
1

@sudo_O 解决方案的扩展(谢谢)

  1. 根据命令行参数从标准输入输出字段,
  2. 按请求的顺序输出字段(可能多次),
  3. 当请求但未找到字段时输出占位符,并且
  4. 警告有关标头中重复字段名称的标准错误。
#!/usr/bin/awk -f
# Process standard input outputting named columns provided as arguments.
#
# For example, given foo.dat containing
#     a b c c
#     1a 1b 1c 1C
#     2a 2b 2c 2C
#     3a 3b 3c 3C
# Running
#   cat foo.dat | ./namedcols c b a a d
# will output
#   1c 1b 1a 1a d
#   2c 2b 2a 2a d
#   3c 3b 3a 3a d
# and will warn on standard error that it
#   Ignored duplicate 'c' in column 4
# Notice that the requested but missing column d contains "d".
#
# Using awk's -F feature it is possible to parse comma-separated data:
#   cat foo.csv | ./namedcols -F, c b a a d
BEGIN {
    for (i=1; i<ARGC; ++i)
        desired[i] = ARGV[i]
    delete ARGV
}
NR==1 {
    for (i=1; i<=NF; i++)
        if ($i in names)
            printf "Ignored duplicate '%s' in column %d\n", $i, i | "cat 1>&2"
        else
            names[$i] = i
    next
}
{
    for (i=1; i<ARGC; ++i)
        printf "%s%s",                                          \
               (i==1 ? "" : OFS),                               \
               ((ndx = names[name = desired[i]])>0 ? $ndx: name)
    printf RS
}
于 2013-11-25T16:26:06.387 回答