-1

I have a file that contains some genetic sequences like this one:

tgcaccaaacatgtctaaagctggaaccaaaattactttctttgaagacaaaaactttcaaggccgccactatgacagcgattgcgactgtgcagatttccacatgtacctgagccgctgactccatcagagtggaaggaggcacctgggctgtgtatgaaaggcccaattttgctgggtacatgtacatcctaccccggggcgagtatcctgagtaccagcactggatgggcctcaacgaccgcctcagctcctgcagggctgttcacctgtctagtggaggccagtataagcttcagatctttgagaaaggggattttaatggtcagatgcatgagaccacggaagactgcccttccatcatggagcatccacatgcgggaggtccactcctgtaaggtgctggagggcgcctggatcttctatgagctgcccaactaccgagcaggcagtacctgctggacaagaaggagtaccggaagcccgtcgactggggtgcagcttccccagctgtccagctttccgccgcattgtggagtgatgatacagatgcggccaaacgctggctggccttgtcatccaaataagcattataaataaaacaattggcatgc

MDITIHNPLIRRPLFSWLAPSRIFDQIFGEHLQESELLPASPSLSPFLMRSPIFRMPSWLETGLSEMRLEKDKFSVNLDVKHFSPEELKVKVLGDMVEIHGKHEERQDEHGFIAREFNRKYRIPADVDPLTITSSLSLDGVLTVSAPRKQSDVPERSIPITREEKPAIAGAQRKMDITIHNPLIRRPLFFSPEELKVKVLGDMVEIHGKHEERQDEHGFIAREFNRKYRIPADVDPLTITSSLSLDGVLTVSAPSRIFDQIFGEHLQESELLPASPSLSPFLMRSPIFRMPSWLETGLSEMRLEKDKFSVNLDVKHFSPEELKVKVLGDMVEIHGKHEERQDEHGFIAREFNRKYRIPADVDPLTITSSLSLDGVLTVSAPRKQSDVPERSIPITREEKPAIAGAQRKMDITIHNPLIRRPLFFSPEE

SWLAPSRIFDQIFGEHLQESELLPASPSLSPFLMRSPIFRMPSWLETGLSEMRLEKDKFSVNLDVKHFSPEELKVKVLGDMVEIHGKHEERQDEHGFIAREFNRKYRIPADVDPLTITSSLSLDGVLTVSAPRKQDVPERSIPITREEKPAIAGAQRIFGEHLQESELLPASPSLSPFLMRSPIFRMPSWLETGLSEMRLEKDKFSVNLDVKHFSPEELKVKVLGDMVEIHGKHEERQDEHGFIAREFNRKYRIPADVDPLTITSSLSLDGVLTVKFGEHLQESELLPASPSLSPFLMRSPIFRMPSWLETGLSEMRLEKDKFSVNLDVKHFSPEELKVKVLGDMVEIHGKHEERQDEHGFIAREFNRKYRIPADVDPLTITSSLSLDGVLTVSAPRKQDVP

Each sequence is represented as a single line. I want to put each line as a set of lines such that each one of them is less than 80 characters in length. How may I do divide each line into a set of lines such that the length is less than 80 characters?

  • Do you want to keep blank lines? – cuonglm Sep 24 '15 at 2:43
  • I could come up with this solution: cat output.txt | sed -r 's/(.{79})/\1\n/g' – Faiz Lotfy Sep 24 '15 at 2:47
  • @cuonglm Yes, I want to keep the bank lines between each sequence. Please notice that my file has 1000s of these sequences. – Faiz Lotfy Sep 24 '15 at 2:49
  • Your title says that you want to combine lines into a single line; the body of your question says you want to break each line into several lines.  And "I want to put each line a set of lines" doesn't make sense — I guess it's missing some word(s) or has some extraneous word(s) inserted. – Scott Sep 24 '15 at 2:57
  • @Scott I apologize for this mistake. How may I edit the question? It should be "How to break a string into smaller strings with a certain limit on their lengths?" – Faiz Lotfy Sep 24 '15 at 3:11
2

Your solution works, just could be simplified, plus the output saved, with feedback:

$ sed -r 's/(.{79})/\1\n/g' output.txt | tee output2.txt
  • sed command can already take file... arguments like output.txt so you could avoid what some consider "feline abuse" :)
  • tee allows you to see the output as well as write the output, thus giving you a form of feedback, otherwise what happens with large files is that nothing will seem to be happening on screen for a while, so it is always good to have feedback.
2

With sed, if your data file is data.txt and you want to output to a file broken.txt :

sed -n 'l 80' data.txt | sed 's/\$\|\\//' > broken.txt

The first part of the pipe makes every line 80 characters long. However, this handy command in sed outputs text in 'unambiguous' form, meaning that line breaks now become $s and the breaks put in by sed are rendered as \s. So the second part of the pipe is to get rid of those.

This solution assumes that there are no \s or $ in data.txt to begin with.

  • (1) Your answer adds backslashes along with newlines.  That might be what the OP wants, but the question doesn't say so.  (2) Your answer will remove the first $ symbol from each line (after splitting), including those that were present in the original file.  The question shows an example file that's all letters, and that might be true in general, but it's hard to be sure. – Scott Sep 24 '15 at 3:39
  • @Scott : Quite right ! I'm so used to my editor breaking lines with \ that I forgot that 'unambiguous' form also renders the page breaks it inserts as \s. Updated answer accordingly. – user5283407 Sep 24 '15 at 4:28
1

When splitting lines like this, it is fairly common practice to indicate that the line has been split (and is intended to be seen/used/read as one line) by either marking the end of the line with a \ character or the start of the split lines with some white space (e.g. 2 space chars) or both.

The following sed script does both and ensures that each line can be no more than 79 characters wide (so suitable for displaying on an 80-column terminal or printer).

sed -r 's/(.{75})/\1 \\\n  /g' geneseq.txt

This not only makes it visually clearer which lines are supposed to be joined up, it also makes it easier to re-use the output if you need to process it some more later.

1

Why anybody offer fold?

fold infile > outfile

From the man page:

NAME
       fold - wrap each input line to fit in specified width

SYNOPSIS
       fold [OPTION]... [FILE]...

use -w to override the default of 80.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.