3

我正在尝试这个的一个小变化除了我告诉 awk 要根据第 5 个字段拆分的文件的分隔符可以是冒号“:”或制表符 \t。我一个人做这awk -F '[:\t]'部分,它确实打印了正确的 $5 字段。

但是,当我尝试将其合并到更大的命令中时,它会返回以下错误:

                                                             print > f
awk: cmd. line:9:                                            ^ syntax error

这是代码:

awk -F '[:\t]' '    # read the list of numbers in Tile_Number_List
    FNR == NR {
        num[$1]
        next
    }

    # process each line of the .BAM file
    # any lines with an "unknown" $5 will be ignored
    $5 in num {
        f = "Alignments_" $5 ".sam"        print > f
    } ' Tile_Number_List.txt little.sam

为什么它不能与 -F 选项一起使用?

4

1 回答 1

2

问题不FS在于错误所指出的这一行的值:

f = "Alignments_" $5 ".sam"        print > f

您在一行上有两个语句,因此可以用 a;或换行符分隔它们:

f = "Alignments_" $5 ".sam"; print > f

或者:

f = "Alignments_" $5 ".sam"
print > f

作为一个完整的班轮:

awk -F '[:\t]' 'FNR==NR{n[$1];next}$5 in n{print > ("Alignments_"$5".sam")}'

或作为脚本文件,即script.awk

BEGIN {
    FS="[:\t]" 
}
# read the list of numbers in Tile_Number_List
FNR == NR {
    num[$1]
    next
}
# process each line of the .BAM file
# any lines with an "unknown" $5 will be ignored
$5 in num {
    f = "Alignments_" $5 ".sam"        
    print > f
}

以这种形式运行awk -f script.awk Tile_Number_List.txt little.sam

编辑:

该字符- 用于表示来自标准输入的输入,而不是具有许多 *nix 工具的文件。

command | awk -f script.awk Tile_Number_List.txt -
于 2013-05-06T18:17:39.893 回答