3

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
    Commented Jun 25, 2020 at 13:21

3 Answers 3

7

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
  • 3
    Or !/^#/ { ... } (but this matches empty lines too)
    – Kusalananda
    Commented Jun 25, 2020 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
    Commented Jun 25, 2020 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
    Commented Jun 25, 2020 at 13:22
4

using sed

sed '/^#/! s/.*/chr&/'
2
  • 4
    s/^/chr/ (doesn't need .*+&)
    – guest
    Commented Jun 25, 2020 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&/' Commented Jun 25, 2020 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
  • Thanks for letting me know will correct it Commented Jun 26, 2020 at 7:56
  • Corrected and added the code Commented Jun 26, 2020 at 8:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .