4

I am trying to remove some junk and substitute words with the key digits, starting with strings like:

>ref|NC_035781.1|  
>ref|NC_035782.1|   
>ref|NC_035783.1|   
>ref|NC_035784.1|   

and wanting to end with:

>NC_035781.1  
>NC_035782.1  
>NC_035783.1  
>NC_035784.1  

I tried

sed 's/\>ref\|NC_03578\([0-9]\)\.1\|/>NC_03578\1\.1/g' chr.test | grep '>NC_'

to test it and got back many more results than expected looking like:

>NC_03578.1>>NC_03578.1r>NC_03578.1e>NC_03578.1f>NC_03578.1|>NC_035780.1|>NC_03578.1 >NC_03578.1C>NC_03578.1r>NC_03578.1a>NC_03578.1s>NC_03578.1s>NC_03578.1o>NC_03578.1s>NC_03578.1t>NC_03578.1r>NC_03578.1e>NC_03578.1a>NC_03578.1   
...  

What am I doing wrong?

1
  • sed 's/ref\||//g' file?
    – Cyrus
    Commented Sep 22, 2019 at 11:08

1 Answer 1

4

The main issue is that you have escaped > and | and I suspect you are using GNU sed. In which case, \> acts as end of word boundary and \| will act as alternation operator.

$ sed 's/>ref|\(NC_03578[0-9]\.1\)|.*/>\1/' chr.test
>NC_035781.1
>NC_035782.1
>NC_035783.1
>NC_035784.1

The modified command is shown above. Also, there seems to be some spaces at end of line, so I've added .* to remove them. Use sed -n '/NC_/ s/>ref|\(NC_03578[0-9]\.1\)|.*/>\1/p' to include the grep operation within sed itself


You can also use awk

$ awk -F'|' '/NC_/{print ">" $2}' chr.test
>NC_035781.1
>NC_035782.1
>NC_035783.1
>NC_035784.1
  • -F'|' to define | as field separator. | is a regexp operator, but when FS (the field separator special awk variable set by -F) is a single character, it is not treated as a regexp. -F'[|]' or -F'\\|' would also work but would be less efficient.
  • /NC_/ if input line contains NC_
    • print ">" $2 print > character and the second field

You must log in to answer this question.

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