AWK:有条件的恢复线

时间:2019-06-20 14:21:00

标签: awk grep

我想通过仅保留与列中的模式匹配的某些行来从另一个文件创建文件。

基本文件的一部分:

"1","rs543921925","ENSG00000187634","ENST00000616125","intron_variant"
"2","rs543921925","ENSG00000187634","ENST00000620200","intron_variant"
"3","rs543921925","ENSG00000187634","ENST00000617307","intron_variant"
"4","rs146327803","ENSG00000187634","ENST00000420190","missense_variant"
"5","rs146327803","ENSG00000187634","ENST00000437963","missense_variant"
"6","rs146327803","ENSG00000187634","ENST00000342066","missense_variant"
"7","rs146327803","ENSG00000187634","ENST00000618181","missense_variant"

我想要的文件

"4","rs146327803","ENSG00000187634","ENST00000420190","missense_variant"
"5","rs146327803","ENSG00000187634","ENST00000437963","missense_variant"
"6","rs146327803","ENSG00000187634","ENST00000342066","missense_variant"
"7","rs146327803","ENSG00000187634","ENST00000618181","missense_variant"

我尝试过:

awk -F'"' '$9 ~ /missense_variant/ { print $0 }'base_file.txt

但这不起作用。

5 个答案:

答案 0 :(得分:2)

我认为有时最好使用实际的文件定界符。

$ awk -F, '$NF=="\"missense_variant\"" base_file.txt

可能就是您想要的。

答案 1 :(得分:0)

您本可以轻松地自己解决这个问题:

$ awk -F'"' 'NR==1{for (i=1; i<=NF; i++) print NF, i, "<" $i ">"}' file
11 1 <>
11 2 <1>
11 3 <,>
11 4 <rs543921925>
11 5 <,>
11 6 <ENSG00000187634>
11 7 <,>
11 8 <ENST00000616125>
11 9 <,>
11 10 <intron_variant>
11 11 <>

请注意,9美元对10美元的价格是多少。

此外,请考虑将其用于FS:

$ awk -F'^"|","|"$' 'NR==1{for (i=1; i<=NF; i++) print NF, i, "<" $i ">"}' file
7 1 <>
7 2 <1>
7 3 <rs543921925>
7 4 <ENSG00000187634>
7 5 <ENST00000616125>
7 6 <intron_variant>
7 7 <>

或:

$ awk -F'","' '{gsub(/^"|"$/,"")} NR==1{for (i=1; i<=NF; i++) print NF, i, "<" $i ">"}' file
5 1 <1>
5 2 <rs543921925>
5 3 <ENSG00000187634>
5 4 <ENST00000616125>
5 5 <intron_variant>

答案 2 :(得分:0)

实际上awk脚本可以解决问题,但是grep更容易,更简单。

您的脚本错误是字段分隔符:

awk -F',' '$5 ~ /missense_variant/ { print }' base_file.txt

效果很好

但是grep更简单

grep "missense_variant\"$" input.txt

答案 3 :(得分:0)

awk '/missense_variant/{print $0}' file

"4","rs146327803","ENSG00000187634","ENST00000420190","missense_variant"
"5","rs146327803","ENSG00000187634","ENST00000437963","missense_variant"
"6","rs146327803","ENSG00000187634","ENST00000342066","missense_variant"
"7","rs146327803","ENSG00000187634","ENST00000618181","missense_variant"

答案 4 :(得分:0)

谢谢您的所有建议,它们的工作效果非常好,我看了最适合我的问题的建议