1

I'd like to print "chr" at the begin of each line that doesn't start with "#" in my file.

input :

##toto
#titi
16
17

output :

##toto
#titi
chr16
chr17

I've tried with awk (awk '$1 ~ /^#/ ... ) or grep (grep "^[^#]" ...) but got no success. How can I accomplish this?

1
  • If you're working with vcf files, remember to always use awk -F'\t' since there can be spaces in the INFO fields. – terdon Jun 25 '20 at 13:21
6

I think you need ^[^#] meaning, starting with a character that is not # and reconstruct those lines by prefixing with "chr"

awk '/^[^#]/{ $0 = "chr"$0 }1'
3
  • 2
    Or !/^#/ { ... } (but this matches empty lines too) – Kusalananda Jun 25 '20 at 13:02
  • thanks ! I have a question , if i want to print with the lign that start with "#" , what do I have to change in awk '/^[^#]/ ? – nstatam Jun 25 '20 at 13:20
  • @nstatam: You can do some trial and error to find that yourself. If /^[^#]/ ignores lines starting with #, then do the opposite, remove the negation. as /^#/ – Inian Jun 25 '20 at 13:22
3

using sed

sed '/^#/! s/.*/chr&/'
2
  • 4
    s/^/chr/ (doesn't need .*+&) – guest Jun 25 '20 at 13:43
  • 2
    sed 's/^[^#]/chr&/' may be preferable to avoid adding the prefix to empty lines if any. Or sed 's/^[[:digit:]]/chr&/' – Stéphane Chazelas Jun 25 '20 at 15:07
0

command

awk '{if(!/^#/ && !/^$/){$0="chr"$0;print}else if (/^#/ && !/^$/){print $0}}' filename

output

##toto
#titi
chr16
chr17

python

#!/usr/bin/python
import re
k=re.compile(r'^#')
e=re.compile(r'^$')
p=open('filename','r')
for i in p:
    if not re.search(k,i)   and not re.search(e,i):
        print "chr{0}".format(i.strip())
    elif not re.search(e,i):
        print i.strip()

output

##toto
#titi
chr16
chr17
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.