This is a test version of Biostars. For the public version, visit https://www.biostars.org.
grep -v command

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.

grep chr1

2 answers

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

Original poster says:

I have a BED file having entries from all chromosome

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.

Log in to answer this question.