Home > Software engineering >  Delete all text after a specific occurrence in a string using bash
Delete all text after a specific occurrence in a string using bash

Time:10-26

I have a sequence:

MALYYDHQIEAPDAAGSPSFISWHPVHPFLAVAYISTTSTGSVDIYLEQGECVPDTHVERPFRVASLCWHPTRLVLAVGWETGEVTVFNKQDKEQHTMPLTHTADITVLRWSPSGNCLLSGDRLGVLLLWRLDQRGRVQGTPLLKHEYGKHLTHCIFRLPPPGEDLVQLAKAAVSGDEKALDMFNWKKSSSGSLLKMGSHEGLLFFVSLMDGTVHYVDEKGKTTQVVSADSTIQMLFYMEKREALVVVTENLRLSLYTVPPEGKAEEVMKVKLSGKTGRRADIALIEGSLLVMAVGEAALRFWDIERGENYILSPDEKFGFEKGENMNCVCYCKVKGLLAAGTDRGRVAMWRKVPDFLGSPGAEGKDRWALQTPTELQGNITQIQWGSRKNLLAVNSVISVAILSERAMSSHFHQQVAAMQVSPSLLNVCFLSTGVAHSLRTDMHISGVFATKDAVAVWNGRQVAIFELSGAAIRSAGTFLCETPVLAMHEENVYTVESNRVQVRTWQGTVKQLLLFSETEGNPCFLDICGNFLVVGTDLAHFKSFDLSRREAKAHCSCRSLAELVPGVGGIASLRCSSSGSTISILPSKADNSPDSKICFYDVEMDTVTVFDFKTGQIDRRETLSFNEQETNKSHLFVDEGLKNYVPVNHFWDQSEPRLFVCEAVQETPRSQPQSANGQPQDGRAGPAADVLILSFFISEEHGFLLHESFPRPATSHSLLGMEVPYYYFTRKPEEADREDEVEPGCHHIPQMVSRRPLRDFVGLEDCDKATRDAMLHFSFFVTIGDMDEAFKSIKLIKSEAVWENMARMCVKTQRLDVAKVCLGNMGHARGARALREAEQEPELEARVAVLATQLGMLEDAEQLYRKCKRHDLLNKFYQAAGRWQEALQVAEHHDRVHLRSTYHRYAGHLEASADCSRALSYYEKSDTHRFEVPRMLSEDLPSLELYVNKMKDKTLWRWWAQYLESQGEMDAALHYYELARDHFSLVRIHCFQGNVQKAAQIANETGNLAASYHLARQYESQEEVGQAVHFYTRAQAFKNAIRLCKENGLDDQLMNLALLSSPEDMIEAARYYEEKGVQMDRAVMLYHKAGHFSKALELAFATQQFVALQLIAEDLDETSDPALLARCSDFFIEHSQYERAVELLLAARKYQEALQLCLGQNMSITEEMAEKMTVAKDSSDLPEESRRELLEQIADCCMRQGSYHLATKKYTQAGNKLKAMRALLKSGDTEKITFFASVSRQKEIYIMAANYLQSLDWRKEPEIMKNIIGFYTKGRALDLLAGFYDACAQVEIDEYQNYDKAHGALTEAYKCLAKAKAKSPLDQETRLAQLQSRMALVKRFIQARRTYTEDPKESIKQCELLLEEPDLDSTIRIGDVYGFLVEHYVRKEEYQTAYRFLEEMRRRLPLANMSYYVSPQAVDAVHRGLGLPLPRTVPEQVRHNSMEDARELDEEVVEEADDDP

I would like to replace index position 383 (Q) with nothing after it, so the desired outcome would be:

MALYYDHQIEAPDAAGSPSFISWHPVHPFLAVAYISTTSTGSVDIYLEQGECVPDTHVERPFRVASLCWHPTRLVLAVGWETGEVTVFNKQDKEQHTMPLTHTADITVLRWSPSGNCLLSGDRLGVLLLWRLDQRGRVQGTPLLKHEYGKHLTHCIFRLPPPGEDLVQLAKAAVSGDEKALDMFNWKKSSSGSLLKMGSHEGLLFFVSLMDGTVHYVDEKGKTTQVVSADSTIQMLFYMEKREALVVVTENLRLSLYTVPPEGKAEEVMKVKLSGKTGRRADIALIEGSLLVMAVGEAALRFWDIERGENYILSPDEKFGFEKGENMNCVCYCKVKGLLAAGTDRGRVAMWRKVPDFLGSPGAEGKDRWALQTPTELQGNITQ

I have tried:

sed 's/"Q"//383' text.file
sed 's/Q//383' text.file

But that does not do anything.

In the long term I need to do with with a few other positions so a method that is flexible would be much appreciated.

All the best

CodePudding user response:

This might work for you (GNU sed):

sed -zEi 's/(.{383}).*/\1/' file

Store the first 383 characters as a back reference and remove the rest.

N.B. the -z option slurps the entire file into memory.

CodePudding user response:

GNU awk:

gawk '{print gensub(/(.{383}).*/,"\\1","1")}' file

CodePudding user response:

You could do this with cut:

cut -c 1-383 < text.file

CodePudding user response:

Just to throw it into the pool, head can be used to cut off after n lines, but also after c bytes:

$ head -c 383 text.file

CodePudding user response:

If you store your string in a bash variable you can use bash parameter expansion:

mystring=$(cat text.file)
echo "${mystring:0:383}"

CodePudding user response:

Another option: dd

dd if=txt.file bs=383 count=1 status=none
  • Related