grep -v command
2
0
Entering edit mode
3.7 years ago
amitpande74 ▴ 20

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 • 1.1k views
ADD COMMENT
5
Entering edit mode
3.7 years ago

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 COMMENT
0
Entering edit mode

Original poster says:

I have a BED file having entries from all chromosome

ADD REPLY
0
Entering edit mode

@genomax opps, updated.

https://xkcd.com/745/

ADD REPLY
1
Entering edit mode
3.7 years ago

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 COMMENT

Login before adding your answer.

Traffic: 2044 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6