我想创建awk代码,它将修改这样的文本:
我有这个代码,但它不好:
#!/bin/bash
for i
in *.vcf;
do
awk 'BEGIN {print "CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILT\tINFO\tFORMAT"}' |
awk '{$1 "\t" $2 "\t" $3 "\t" $4 "\t" $5 "\t" $6 "\t" $7 "\t" $8 "\t" $9}' $i |
awk '!/#/' > ${i%.vcf}.tsv;
done
Run Code Online (Sandbox Code Playgroud)
INPUT:
> ##fileformat=VCFv4.1
> ##FORMAT=<ID=GQX,Number=1,Type=Integer,Description="Minimum of {Genotype quality assuming variant position,Genotype quality assuming
> non-variant position}">
> #CHROM POS ID REF ALT QUAL FILTER INFO FORMAT 1 chr1 10385471 rs17401966 A G 100.00 PASS DP=67;TI=NM_015074;GI=KIF1B;FC=Silent GT:GQ:AD:VF:NL:SB:GQX 0/1:100:29,38:0.5672:20:-100.0000:100
> …
Run Code Online (Sandbox Code Playgroud) 我有一个看起来像这样的文本文件:
test10 2016-05-30 207
test11 2016-06-01 207
test12 2016-07-20 207
test13 2016-07-21 207
test14 2016-07-25 207
Run Code Online (Sandbox Code Playgroud)
如果该日期超过30天,我想从文本文件中删除这些行.我怎样才能做到这一点?我已经读了一些sed
但是不确定它是否可以完成或者如何去做.
我想编写一个接受两个参数的函数,一个是常量值,另一个是数组。该函数查找数组中元素的索引并返回它。我想使用多个数组调用此函数,如下我所尝试的那样。
BEGIN{
a[1]=2;
a[2]=4;
a[3]=3;
b[1]=4;
b[2]=2;
b[3]=6;
c[1]=5;
c[2]=1;
c[3]=6;
arr[1]=a;
arr[2]=b;
arr[3]=c
}
function pos(val,ar[]) {
for (m=1;m<=length(ar);m++) { if (val == ar[m] )
return m;
else continue }
}
{for( k=1;k<=NF;k++) { for(l=1;l<=length(arr);l++) { print "pos=" pos($i,arr[l])} } }
Run Code Online (Sandbox Code Playgroud)
但我收到错误:
fatal: attempt to use array `a' in a scalar context
Run Code Online (Sandbox Code Playgroud)
查看代码,任何人都可以告诉我如何使用 awk 实现我想要实现的目标。我在这里面临的挑战是将数组作为元素分配给另一个数组,如在 中arr[1]=a
,并通过使用其索引引用该数组作为参数传递该数组,如在 中pos($i,arr[l]
。我不知道如何在 awk 中使这些语句在语法和功能上正确。
输入是:
2 4 6
3 5 6
1 2 5
Run Code Online (Sandbox Code Playgroud)
在输出中,如果从文件中读取的值存在于任何定义的数组中,则代码应返回该值的位置
输出:
1 1 3
6 …
Run Code Online (Sandbox Code Playgroud) 我有一个主文件,其中只有一个匹配的字符串,我想更改匹配短语后面第二行中的一列的值,并将其输出到一个单独的文件。我有另一个参考文件,其中包含输出文件名(第一列)和替换值(第二列)。下面是示例和我尝试的代码,有错误且没有任何输出。我感谢您的支持。
(主文件)
some words here
This is the 'MATCH LINE'
# this is just a comment
This NUMBER to be updated
and other words here
Run Code Online (Sandbox Code Playgroud)
(参考文件)
Out1 ONE
Out2 TWO
Out3 THREE
Run Code Online (Sandbox Code Playgroud)
(预期输出文件:Out1)
some words here
This is the 'MATCH LINE'
# this is just a comment
This ONE to be updated
and other words here
Run Code Online (Sandbox Code Playgroud)
(预期输出文件:Out2)
some words here
This is the 'MATCH LINE'
# this is just a comment
This TWO to be updated
and other words here …
Run Code Online (Sandbox Code Playgroud) 嗨专家我有一个如下所示的文件我想根据块的最大长度通过在预期答案中给出的末尾附加零来均衡由 > 符号分隔的所有列块。谢谢。
>
1 2 3
3 4 5
>
3 4 5
>
2 3 4
3 4 5
3 4 5
Run Code Online (Sandbox Code Playgroud)
预期产出
>
1 2 3
3 4 5
0 0 0
>
3 4 5
0 0 0
0 0 0
>
2 3 4
3 4 5
3 4 5
Run Code Online (Sandbox Code Playgroud)