Haikal Yeo
Haikal Yeo

Reputation: 247

Concatenate 2000 lines into 1 line in Emacs

I have 2000 lines of code that I need to concatenate, hopefully into one.

I know that in Emacs, with the replace-string command, I can add the + for concatenation at the back of each line all in one go. But I when I do this I constantly get a syntax error when I'm using the file on SAGE. So I started experimenting and found that if I move them all together on the same line and add the + symbol, it then works. What's the best solution to put them all together on one line?

It might be because I have brackets within brackets in each line. Here are three example lines from my output:

[[['37a1', 1], 153, 160, [[13, 2], [53, -1], [67, 2], [127, -1], [443, -1], [547, 2], [599, -1]]], [['43a1', 1], 159, 163, [[5, -1], [103, -1], [127, -1], [541, -1]]], [['53a1', 1], 159, 161, [[71, -1], [97, -1]]], [['57a1', 1], 155, 157, [[5, 2], [11, -1]]], [['58a1', 1], 152, 157, [[31, 2], [53, -1], [109, -1], [673, -1], [739, -1]]], [['61a1', 1], 157, 161, [[7, -1], [13, -1], [71, 2], [113, -1]]], [['65a1', 1], 159, 160, [[43, 2]]], [['77a1', 1], 155, 160, [[5, 2], [31, -1], [71, -1], [179, -1], [223, -1]]], [['79a1', 1], 156, 159, [[41, 2], [83, 2], [131, 2]]], [['82a1', 1], 150, 154, [[5, 2], [229, 2], [283, 2], [499, 2]]]]
[[['112a1', 1], 155, 156, [[5, -1]]], [['117a1', 1], 155, 157, [[11, 2], [523, 2]]], [['118a1', 1], 158, 161, [[127, -1], [251, -1], [277, -1]]], [['121b1', 1], 82, 82, []], [['122a1', 1], 151, 158, [[5, -1], [43, 2], [79, -1], [113, -1], [191, -1], [241, 2], [523, -1]]], [['123a1', 1], 157, 159, [[5, 0], [7, 2]]], [['123b1', 1], 156, 159, [[17, 2], [29, -1], [811, -1]]], [['124a1', 1], 155, 155, []], [['128a1', 1], 161, 162, [[13, 2]]], [['129a1', 1], 160, 163, [[283, -1], [563, -1], [659, -1]]]]
[[['130a1', 1], 158, 159, [[43, 2]]], [['131a1', 1], 161, 163, [[59, 0], [271, -1]]], [['135a1', 1], 151, 158, [[19, -1], [53, 2], [151, -1], [241, -1], [421, -1], [607, -1], [613, -1]]], [['136a1', 1], 160, 161, [[5, 2]]], [['138a1', 1], 155, 157, [[11, 2], [313, 2]]], [['141a1', 1], 154, 159, [[7, 0], [29, -1], [37, -1], [97, -1], [557, -1]]], [['141d1', 1], 159, 161, [[37, -1], [619, -1]]], [['142a1', 1], 152, 157, [[5, -1], [13, -1], [131, -1], [233, 2], [907, 2]]], [['142b1', 1], 158, 160, [[31, 0], [911, -1]]], [['143a1', 1], 155, 159, [[7, 2], [29, 2], [113, -1], [509, 2]]]]

So what I want is something like

A = B + C + D + ... = [a, b, c, ...] + [d, e, f, ...] + ...

where

[['37a1', 1], 153, 160, [[13, 2], [53, -1], [67, 2], [127, -1], [443, -1], [547, 2], [599, -1]]]

is one example of such an entry a and B, C, D are the set of entries of lines 1, 2 and 3 (and I have 2000 of such lines).

Upvotes: 1

Views: 283

Answers (2)

kjhughes
kjhughes

Reputation: 111491

To go from

A
B
C

to

A+B+C

You could replace line endings with +'s:

M-x replace-stringRETC-q C-jRET+RET

...and then manually fix-up the start and end of the resulting single line as necessary to meet the needs of a proper SAGE statement.

Upvotes: 4

Tom Tromey
Tom Tromey

Reputation: 22519

There are lots of ways to do this but a simple one is keyboard macros.

Move to the first line. Use C-x ( to start a keyboard macro.

Then C-e C-k to join the next line to the current line.

Now C-x ) to end the keyboard macro.

Now you can run it as much as you want: M-1000 C-x e.

If you want to more easily limit it you can narrow the buffer to the area you want to operate on.

Upvotes: 3

Related Questions