Question: grep -v command
0
gravatar for amitpande74
6 weeks ago by
amitpande740 wrote:

Hi, I have a BED file having entries from all chromosome and would like to remove chr1 while retaining all other entires which start with chr11 chr19 and so on. When I use grep -v "chr1" it removes the other chromosomes as well which start with chr11 or chr19. Is there a regular expression which I can use to avoid this? Kindly help.

chr1 grep • 187 views
ADD COMMENTlink modified 6 weeks ago by Pierre Lindenbaum130k • written 6 weeks ago by amitpande740
5
gravatar for Pierre Lindenbaum
6 weeks ago by
France/Nantes/Institut du Thorax - INSERM UMR1087
Pierre Lindenbaum130k wrote:

Hi, I have a BED file having entries from all chromosome and would like to remove chr1 while retaining all other entires which start with chr11 chr19 and so on

using grep is not a good choice. Because the word 'chr1' could be present in another column.

using awk:

awk '$1!="chr1"' input.vcf
ADD COMMENTlink modified 6 weeks ago • written 6 weeks ago by Pierre Lindenbaum130k

Original poster says:

I have a BED file having entries from all chromosome

ADD REPLYlink written 6 weeks ago by genomax89k

@genomax opps, updated.

https://xkcd.com/745/

ADD REPLYlink modified 6 weeks ago • written 6 weeks ago by Pierre Lindenbaum130k
1
gravatar for cpad0112
6 weeks ago by
cpad011214k
India
cpad011214k wrote:

Try with -w or word boundary and ^ anchor.

$ cat test.txt 
chr1    chr111
chr11   chr1
chr21   chr1
Chr1    chr100

$ grep -wv "^chr1" test.txt 
chr11   chr1
chr21   chr1
Chr1    chr100

$ grep -v '^\bchr1\b' test.txt 
chr11   chr1
chr21   chr1
Chr1    chr100


$ sed -n '/^chr1\t/!p' test.txt 
chr11   chr1
chr21   chr1
Chr1    chr100

Instead of using generic tools, please use dedicated tools such as bedops for operations on bed files. In this case, function bedextract from bedops may be helpful.

ADD COMMENTlink modified 6 weeks ago • written 6 weeks ago by cpad011214k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1117 users visited in the last hour