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SKALELAFATQQFVALQLIAEDLDETSDPALLARCSDFFIEHSQYERAVELLLAARKYQEALQLCLGQNMSITEEMAEKMTVAKDSSDLPEESRRELLEQIADCCMRQGSYHLATKKYTQAGNKLKAMRALLKSGDTEKITFFASVSRQKEIYIMAANYLQSLDWRKEPEIMKNIIGFYTKGRALDLLAGFYDACAQVEIDEYQNYDKAHGALTEAYKCLAKAKAKSPLDQETRLAQLQSRMALVKRFIQARRTYTEDPKESIKQCELLLEEPDLDSTIRIGDVYGFLVEHYVRKEEYQTAYRFLEEMRRRLPLANMSYYVSPQAVDAVHRGLGLPLPRTVPEQVRHNSMEDARELDEEVVEEADDDP

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

3
  • 2
    A simple substr in awk would work here, like: awk '{print substr($0,1,383)}' Input_file. Commented Oct 25, 2021 at 12:54
  • 1
    sed 's/Q//383' text.file prompts sed to remove 383th occurence of Q, as there is only 69 Qs no action is taken Commented Oct 25, 2021 at 14:02
  • Please specify whether the file has any kind of line delimiter, or is just one long string of capital letters. Commented Oct 27, 2021 at 1:34

7 Answers 7

4

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.

Sign up to request clarification or add additional context in comments.

Comments

4

You could do this with cut:

cut -c 1-383 < text.file

Comments

3

GNU awk:

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

Comments

2

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

Comments

1

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

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

Comments

1

Another option: dd

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

Comments

1

Using bash and printf:

 printf '%.383s\n' $(<text.file)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.