how to delete extra character in a line?

And I want to delete the characters longer than 20 for each line start with #. The other lines should remain the same. I think this can be done by sed. Could anyone help me with this? Thanks!

my input file:

#ZP_05494889.1_Clostridium_papyrosolvens
-------SMISALDIIDSYIKNKD-INNVLLVSSCMITPCAREDDIVVYPSTADASAAVI
L--QKITEKNRSGVIDSGFYTDCSYNWTIKNPACGFSR--ITDNSVDTRMKMMEWNQFD-

#YP_002507159.1_Clostridium_cellulolyticum_H10
-------SMISALDIIDSYIKNKN-INNVLLVSSCMITPFAREDDIIVYPSTADASAAVV
L--QKIPDSDRSGVIDSGFYTDCSYNWTIKNPACGFSR--ITDNSVDTNMKMMEWNQFD-

expected output file:

#ZP_05494889.1_Clos
-------SMISALDIIDSYIKNKD-INNVLLVSSCMITPCAREDDIVVYPSTADASAAVI
L--QKITEKNRSGVIDSGFYTDCSYNWTIKNPACGFSR--ITDNSVDTRMKMMEWNQFD-

#YP_002507159.1_Clos
-------SMISALDIIDSYIKNKN-INNVLLVSSCMITPFAREDDIIVYPSTADASAAVV
L--QKIPDSDRSGVIDSGFYTDCSYNWTIKNPACGFSR--ITDNSVDTNMKMMEWNQFD-

Try:

sed 's/^\(#.\{19\}\).*/\1/' file

wow~ that works!
It will be great if you could also explain the code to me, since I'm a beginner in this.

Thanks so much!

If sed finds a # followed by 19 characters ( .\{19\} ) at the start of the line ( ^ ) and then the remaining characters until the end of the line, it replaces all this with the part of the match that lies between \( and \) by using backreference 1 ( \1 ) .

I can imagine this is still a bit cryptic. I hope it is clear enough :slight_smile: