user2380782
user2380782

Reputation: 1446

remove lines with sed and regexp

I have a file that looks like this:

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

I would like to remove the lines with the pattern chr*_*_hap*. Only the last 3 lines should remain in my example. I have tried with the following commands but they don't work:

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

I not very good using regexp

Upvotes: 1

Views: 48

Answers (1)

riteshtch
riteshtch

Reputation: 8769

$ 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

or using 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

Using 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

Upvotes: 2

Related Questions