用sed和regexp删除行

时间:2016-05-20 10:07:49

标签: awk sed

我有一个看起来像这样的文件:

rs994321    -   chr6_ssto_hap7  712891  G   A   0.011180599999999999        0.0058201   62357
rs994321    -   chr6_mcf_hap5   675532  G   A   0.011180599999999999    0.0058201   62357
rs994321    -   chr6_mann_hap4  675338  G   A   0.011180599999999999    0.0058201   62357
rs994321    -   chr4_dbb_hap3   675681  G   A   0.011180599999999999    0.0058201   62357
rs994321    -   chr4_cox_hap2   891136  G   A   0.011180599999999999    0.0058201   62357
rs994321    -   chr6    29372356    G   A   0.011180599999999999    0.0058201   62357
rs9943219   +   chr1    238691947   A   G   0.00700761  0.00727069  62357
rs9943217   +   chr1    238691673   A   G   0.00663929  0.00715566  62357

我想删除带有chr*_*_hap*模式的行。我的例子中只剩下最后3行。我尝试过以下命令,但他们不能工作:

sed '/chr[0-9]_*_hap[0-9]/d' test.txt
sed '/*_hap[0-9]/d' test.txt
sed '/\*_hap[0-9]/d' test.txt

我使用regexp

并不是很好

1 个答案:

答案 0 :(得分:2)

$ egrep -v '\bchr([^_]*_){2}hap[0-9]\b' data
rs994321    -   chr6    29372356    G   A   0.011180599999999999    0.0058201   62357
rs9943219   +   chr1    238691947   A   G   0.00700761  0.00727069  62357
rs9943217   +   chr1    238691673   A   G   0.00663929  0.00715566  62357

或使用sed

$ sed -r '/\bchr([^_]*_){2}hap[0-9]\b/d' data
rs994321    -   chr6    29372356    G   A   0.011180599999999999    0.0058201   62357
rs9943219   +   chr1    238691947   A   G   0.00700761  0.00727069  62357
rs9943217   +   chr1    238691673   A   G   0.00663929  0.00715566  62357

使用awk

$ awk '! /chr[^_]*_[^_]*_hap[0-9]/' data
rs994321    -   chr6    29372356    G   A   0.011180599999999999    0.0058201   62357
rs9943219   +   chr1    238691947   A   G   0.00700761  0.00727069  62357
rs9943217   +   chr1    238691673   A   G   0.00663929  0.00715566  62357