3

This question already has an answer here:

I would like to be able to generate a large number of digits (for example, the first one million decimal digits of the Napier constant) and then save the number in a text file without any line breaks.

I cannot seem to be able to do this in either GEdit or LibreOffice Writer though. I use Maxima on Terminal to generate the numbers, but they are outputted with line breaks.

I either need to find a way to generate the numbers without line breaks, or find a text editor that can handle removing the line breaks from large numbers containing millions of digits (I can remove the line breaks from numbers containing thousands of digits without any problems).

I use the Terminal syntax below to generate the first one million decimal digits of the Napier constant, and then copy and paste the digits into a text editor (removing \\ and \n).

sudo apt-get install wxmaxima

maxima

bfloat(%e),fpprec:1000000;

What happens when I attempt to copy and paste into GEdit and then remove the line breaks, is I can get the digits all on one line, but the digits will not display. Since GEdit can successfully remove the line breaks from and display 1000 digits but not one million digits, I am wondering if it might be a memory issue. However, when I attempt to save the file as TXT and then open it in LibreOffice instead, it still contains line breaks. LibreOffice cannot seem to remove the line breaks at all, even when I copy and paste directly from Terminal.

I should add maybe that I am not on Canonical Ubuntu, but the fork known as Mint :-/ .

marked as duplicate by George Udosen, waltinator, David Foerster, Fabby, Eric Carvalho Dec 26 '17 at 15:37

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    Are you sure you are not just seeing the effect of your text editor's line-wrapping? what do you actually want to do with the number? – steeldriver Dec 25 '17 at 11:54
  • Per my chat with PerlDuck, both GEdit and Perl Chomp were able to successfully remove the line breaks. The reason I thought it was not working is because LibreOffice has a bug (or feature) that automatically inserts line breaks every 10,000 characters when you import a TXT file that is all on one line. I was still able to save the number as a PDF file though without line breaks by opening the TXT file in Google Chrome and then using Print to PDF. – Nicole Sharp Dec 25 '17 at 12:44
  • 1
    "When I find myself in times of trouble/K&R they come to me/Speaking words of wisdom:/Code in C." Or any other language, really. Also see the comment about the editor/word processor doing line wrapping when you look at the file. If you generate 1 million digits with no line breaks, the file size should be 1 million bytes. – jamesqf Dec 25 '17 at 18:11
  • Note that Mint is not an official Ubuntu flavour. Questions beyond just simple text processing should be asked on Unix & Linux. – wjandrea Dec 26 '17 at 1:06
4

GEdit (and Libreoffice) aren't the right tools for this.

If I understand you right, then you want to convert

123456\n

into

123456

Use Perl, for instance:

perl -pe 'chomp' your_input_file.txt > without_newlines.txt

Update The input file turned out to have not only linebreaks, but also a line continuation character \ at the end, i.e.:

2.7182818284590452353602874713526624977572470936999595749669676277240766\
303535475945713821785251664274274663919320030599218174135966290435729003342952\
605956307381323286279434907632338298807531952510190115738341879307021540891499\
348841675092447614606680822648001684774118537423454424371075390777449920695517\
...

To remove them (as well as the linebreaks) the following command will do:

perl -pe 'chomp; s/\\//;' your_input_file.txt > without_newlines.txt
  • I am also not sure that \n is being used either. LibreOffice cannot find any instances of \n being used. I also tried searching for \r with zero results. I think it might be some other kind of hidden line break being generated perhaps. – Nicole Sharp Dec 25 '17 at 11:47
  • 1
    You don't need any programming skills for this. Open a terminal window, goto the directory where your input file is (with the linebreaks) and issue above command. You'll end up with a second file without linebreaks. It's quite simple. – PerlDuck Dec 25 '17 at 11:47
  • I tried it. Still waiting for the new text file that was generated to load into the text editor. Any idea why it takes so long to load a 1-MB plaintext file when I have 8 GB of RAM? :-( – Nicole Sharp Dec 25 '17 at 11:53
  • 3
    That's because it's all in one line. GEdit isn't made for so extremely long lines. While waiting, you can issue wc without_newlines.txt. It shows the number of linebreaks, words, and characters in your file. It should output 0 1 1000000 in your case. – PerlDuck Dec 25 '17 at 11:58
  • I was able to load the new text file in LibreOffice Writer. However, LibreOffice Writer still shows the same line breaks that I would get when I removed \\ and \n in GEdit. There is some other kind of line break being generated by Maxima or Terminal that is not \n and Perl Chomp doesn't recognize. – Nicole Sharp Dec 25 '17 at 12:10
5

IMHO the right way to do this is to avoid copy-pasting the text altogether, by writing the maxima output directly to file:

x:bfloat(%e),fpprec:1000000$
stringout("my_big_number.txt",x);

$ in place of ; in the first expression just suppresses the - rather large - console output.

You can confirm there are no additional newlines in the output file using wc:

$ wc my_big_number.txt
      2       1 1000006 my_big_number.txt

(in fact, there's one additional newline, since stringout appears to insert a single blank line before the output; however there are none within the numerical output itself).

You can then convert the text file to whatever other format you wish - since you have LibreOffice installed, using its standalone conversion filters:

$ libreoffice --convert-to pdf my_big_number.txt
$ xdg-open my_big_number.pdf 

Alternatively you could use LO's unoconv command-line utility:

$ unoconv -f pdf my_big_number.txt 

See for example How to convert TXT to PDF?

  • 1
    On my system (17.04) unoconv isn't installed by default but libreoffice --convert-to pdf meganapier.txt does (partially) work: the bug/feature mentioned by the OP still applies, i.e. it produces a PDF with 100 paragraphs (10.000 characters each). Seems to be a limitation of LibreOffice. But +1 for the better maxima command. – PerlDuck Dec 25 '17 at 15:41
  • Hmm... I'm also on 17.04 (w/ LibreOffice LibreOffice 5.3.1.2 and unoconv 0.7) and I don't seem to have that issue with either conversion method - as far as I can tell, the PDF is a single contiguous paragraph (of 165 pages + change with US letter defaults) – steeldriver Dec 25 '17 at 16:13
  • libreoffice --version gives LibreOffice 5.3.1.2 30m0(Build:2). I use A4 paper size and get 196 pages. At the bottom of page 2 one line is 1 character shorter than the other lines. I marked, copied, and pasted the text up to there into a regular editor and that were 10.000 characters. However. – PerlDuck Dec 25 '17 at 16:36
  • I actually like the first way better. Outputting directly from Maxima should be cleaner and better in theory, but it is more difficult to open the generated TXT file in GEdit this way (since the line breaks are already removed), and LibreOffice still places extra line breaks in the file (both TXT and PDF). Is there another GUI text editor I can use that won't crash like GEdit does for large single-line text files? – Nicole Sharp Dec 25 '17 at 16:40
  • 1
    @NicoleSharp I ask again what do you actually want to do with the number? why do you need to open it in a text editor? – steeldriver Dec 25 '17 at 16:51

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