【问题标题】:While read line, awk $line and write to variable读取行时,awk $line 并写入变量
【发布时间】:2013-04-30 17:43:05
【问题描述】:

我正在尝试根据第五个字段的值将文件拆分为不同的较小文件。一个非常好的方法是 already suggestedalso here

但是,我试图将其合并到 qsub 的 .sh 脚本中,但没有取得多大成功。

问题是在指定输出该行的文件的部分中,

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

,我需要传递一个在脚本前面声明的变量,它指定了应该写入文件的目录。当我为多个文件发送数组作业时,我需要使用为每个任务构建的变量来执行此操作。

所以说$output_path = ./Sample1

我需要写一些类似的东西

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

但它似乎不喜欢有一个不属于 awk 的 $field 的 $variable。我什至不认为它喜欢在 $5 之前和之后有两个“字符串”。

我返回的错误是它需要拆分文件的第一行 (little.sam) 并尝试像这样命名 f,然后是 /Alignments_" $5 ".sam"(最后三个正确地放在一起)。它自然会说它太大了。

我怎样才能写出它才能工作?

谢谢!

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

更新,在 AWK 添加 -V 并声明变量 OPATH 之后

input=$1
outputBase=${input%.bam}

mkdir -v $outputBase\_TEST

newdir=$outputBase\_TEST

samtools view -h $input | awk 'NR >= 18' | awk -F '[\t:]' -v opath="$newdir" '

FNR == NR {
    num[$1]
    next
}

$5 in num {
    f = newdir"/Alignments_"$5".sam";
    print > f
} ' Tile_Number_List.txt -

mkdir: created directory little_TEST'
awk: cmd. line:10: (FILENAME=- FNR=1) fatal: can't redirect to `/Alignments_1101.sam' (Permission denied)

【问题讨论】:

  • 尝试在$5 ".sam" 之后添加; 或将print >f 换成新的一行。
  • 总是引用您的 shell 变量,除非您有非常具体的理由不这样做并且完全了解使用未引用变量时发生的文件名通配符、通配符扩展等的警告和后果.另外,\_ 打算在$outputBase\_TEST 中做什么?我认为您可能正在尝试写"${outputBase}/_TEST",但根本不清楚。

标签: unix variables awk split field


【解决方案1】:

awk 变量就像 C 变量 - 只需通过名称引用它们即可获取它们的值,无需像使用 shell 变量那样在它们前面加上“$”:

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 {
    output_path = "./Sample1/"
    f = output_path "Alignments_" $5 ".sam"
    print > f
} ' Tile_Number_List.txt little.sam

【讨论】:

    【解决方案2】:

    要将$output_path 等shell 变量的值传递给awk,您需要使用-v 选项。

    $ output_path=./Sample1/
    
    $ awk -F '[:\t]' -v opath="$ouput_path" '    
        # 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 = opath"Alignments_"$5".sam"
            print > f
        } ' Tile_Number_List.txt little.sam
    

    此外,您的脚本中仍然存在来自 previous question 的错误

    编辑:

    使用-v 创建的awk 变量是obase,但你使用newdir 你想要的是:

    input=$1
    outputBase=${input%.bam}
    mkdir -v $outputBase\_TEST
    newdir=$outputBase\_TEST
    
    samtools view -h "$input" | awk -F '[\t:]' -v opath="$newdir" '
    FNR == NR && NR >= 18 {
        num[$1]
        next
    }    
    $5 in num {
        f = opath"/Alignments_"$5".sam"   # <-- opath is the awk variable not newdir
        print > f
    }' Tile_Number_List.txt -
    

    您还应该将NR &gt;= 18 移动到第二个awk 脚本中。

    【讨论】:

    • +1,因为您一定忘记了您为“如何使用 shell var 和 awk”发布了多少答案 :)
    • @Kent haha​​ 它确实经常出现,可能会像每个php 使用正则表达式解析 HTML 的问题一样更糟。
    • 谢谢,sudo_O 在附加 -v 并声明变量后,我仍然收到错误消息。我已将此新错误附加到我的问题中。
    • @kent 和 sudo_O :( 这些是作为菜鸟的缺点,必须处理它!:(
    • @CarmenSandoval 您在 awk 脚本中使用了 newdir,它是 shell 变量。您使用-v 创建的awk 变量是opath
    猜你喜欢
    • 2013-04-26
    • 2018-04-25
    • 2013-04-30
    • 2014-10-15
    • 1970-01-01
    • 2013-05-12
    • 1970-01-01
    • 2012-10-06
    • 1970-01-01
    相关资源
    最近更新 更多